Iteration
Iteration
is simply the repetition
of a block of code in a program. In many cases, we need a control variable
to implement iteration. Alternatively, we can repeatedly execute a block of code until a certain stopping criterion is met.
Generally you can implement iteration using for, while or do-while loop constructs (in some programming languages we have the alternate repeat construct). Let's look at an example pseudo-code, where we print numbers from 1 to 5 using a control variable i.
As the values of i
change in the loop, the numbers 1, 2, 3, 4, 5
are printed. The loop is executed as long as the condition i <= 5
holds true.
xxxxxxxxxx
let i = 1; // Initial value of control variable
// Repeat the next block till condition i <= 5 is true
while (i <= 5) {
console.log(i); // Print i
i = i + 1; // Increment i by 1
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment