A for loop is used when a loop is to be executed a known number of times.
The following applet is here to illustrate you the basic differences between the 3 loop statements. With this you can, e.g , see the difference of execution begin and stop between while and repeat-until.
In the first box, you can enter a for loop code pice and when you click onto the 'Start For' button, you see the output in the 4. box. The same applies for the while-loop and the 2.box and the repeat-until loop and the 3.box.
For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10.
Loops are a way to repeat the same code multiple times.
The for…of and for…in loops
A small announcement for advanced readers.
This article covers only basic loops:
If you came to this article searching for other types of loops, here are the pointers:
Otherwise, please read on.
6 loop has the following syntax:
0 is truthy, the
1 from the loop body is executed.
For instance, the loop below outputs
A single execution of the loop body is called an iteration. The loop in the example above makes three iterations.
Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by
For instance, a shorter way to write
Curly braces are not required for a single-line body
If the loop body has a single statement, we can omit the curly braces
The condition check can be moved below the loop body using the
The loop will first execute the body, then check the condition, and, while it’s truthy, execute it again and again.
This form of syntax should only be used when you want the body of the loop to execute at least once regardless of the condition being truthy. Usually, the other form is preferred:
1 loop is more complex, but it’s also the most commonly used loop.
It looks like this:
Let’s learn the meaning of these parts by example. The loop below runs
4 up to (but not including)
Let’s examine the
1 statement part-by-part:partbegin
7Executes once upon entering the loop.condition
3Checked before every loop iteration. If false, the loop stops.body
2Runs again and again while the condition is truthy.step
4Executes after the body on each iteration.
The general loop algorithm works like this:
1 executes once, and then it iterates: after each
4 are executed.
If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper.
Here’s exactly what happens in our case:
Inline variable declaration
Here, the “counter” variable
2 is declared right in the loop. This is called an “inline” variable declaration. Such variables are visible only inside the loop.
Instead of defining a variable, we could use an existing one:
Any part of
1 can be skipped.
For example, we can omit
1 if we don’t need to do anything at the loop start.
We can also remove the
This makes the loop identical to
We can actually remove everything, creating an infinite loop:
Please note that the two
1 must be present. Otherwise, there would be a syntax error.
Normally, a loop exits when its condition becomes falsy.
But we can force the exit at any time using the special
For example, the loop below asks the user for a series of numbers, “breaking” when no number is entered:
2 directive is activated at the line
4 if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely,
The combination “infinite loop +
2 as needed” is great for situations when a loop’s condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body.
7 directive is a “lighter version” of
2. It doesn’t stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).
We can use it if we’re done with the current iteration and would like to move on to the next one.
The loop below uses
7 to output only odd values:
For even values of
7 directive stops executing the body and passes control to the next iteration of
1 (with the next number). So the
5 is only called for odd values.
A loop that shows odd values could look like this:
From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an
5 block instead of using
But as a side effect, this created one more level of nesting (the
5 call inside the curly braces). If the code inside of
5 is longer than a few lines, that may decrease the overall readability.
Please note that syntax constructs that are not expressions cannot be used with the ternary operator
0. In particular, directives such as
9 aren’t allowed there.
For example, if we take this code:
…and rewrite it using a question mark:
…it stops working: there’s a syntax error.
This is just another reason not to use the question mark operator
0 instead of
Sometimes we need to break out from multiple nested loops at once.
For example, in the code below we loop over
5, prompting for the coordinates
We need a way to stop the process if the user cancels the input.
0 would only break the inner loop. That’s not sufficient – labels, come to the rescue!
A label is an identifier with a colon before a loop:
1 statement in the loop below breaks out to the label:
In the code above,
2 looks upwards for the label named
3 and breaks out of that loop.
So the control goes straight from
We can also move the label onto a separate line:
7 directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop.
Labels do not allow to “jump” anywhere
Labels do not allow us to jump into an arbitrary place in the code.
For example, it is impossible to do this:
2 directive must be inside a code block. Technically, any labelled code block will do, e.g.:
…Although, 99.9% of the time
2 is used inside loops, as we’ve seen in the examples above.
7 is only possible from inside a loop.
We covered 3 types of loops:
To make an “infinite” loop, usually the
03 construct is used. Such a loop, just like any other, can be stopped with the
If we don’t want to do anything in the current iteration and would like to forward to the next one, we can use the
9 support labels before the loop. A label is the only way for
9 to escape a nested loop to go to an outer one.
What loop should be used when the number of loops is known?
For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.
Is for loop used when the number of iterations is unknown?
That's why it is said that 'for' loop is used when number of iterations known and 'while' loop is used when number of iterations are unknown.
What is for loop also known as?
For some programming objectives it is necessay to repeat a set of statements a number of times until a certain condition is fulfilled. In such situations iteration statements can be used. The iteration statements are also called loops or looping statements.
Why is it called a for loop?
The For loop is used in many imperative programming languages notably C and C++ and comes from the English word 'for' which is used to state the purpose of an object or action, in this case the purpose and details of the iteration.