Loops

Raheelanjum
1 min readJust now

A loop is used to repeat a specific block of code over and over. There are two main types of loops: for loops and while loops.

Loops—or repeating yourself

Loops allow us to repeat a single (or several) lines of code over and over again. This allows us to "write once" and then "execute many times."

There are TWO loops that you must memorize.

The For Loop: Which is used when we know how many times the loop will execute.

The While Loop: When we don't know how many times, but want to continue until a certain condition is not true.

Nesting Loops

It is perfectly legal to place the code for one loop inside the code of another loop. What this means is that the "inner" loop is executed one time in its entirety for every time the outer loop executes. Consider the following pseudocode where the outer loop executes 10 times and the inner loop executes 10 times; the code inside the inner loop is executed 100 times!

Nesting is a powerful tool when you want the computer to "process" in "two dimensions.".

--

--

Raheelanjum
Raheelanjum

Written by Raheelanjum

Highly skilled and experienced content writer dedicated to crafting compelling, informative content that resonates with diverse audiences. Expertise spans blog

No responses yet