![]() |
VOOZH | about |
A for loop allows you to repeat a block of code a specific number of times.
1 2 3 4 5
A for loop has three main parts in most languages: initialization, condition, and update.
In Python, a for loop is slightly different. It loops directly over an iterable and has two main parts: variable and iterable.
For loops can appear in different forms depending on the programming language and the task being performed. The following are some common types of for loops.
1. Basic For Loop
The basic for loop is the most commonly used type. It contains initialization, condition, and update expressions and is used when the number of iterations is known.
2 4 6 8 10
2. For Each Loop
The for-each loop is used to iterate directly over elements of a collection such as arrays or lists without using an index.
1 2 3 4 5
3. For Loop with Multiple Variables
Some languages like C, C++, and Java allow multiple loop control variables in a for loop.
i=0, j=10 i=1, j=9 i=2, j=8 i=3, j=7 i=4, j=6
4. Infinite For Loop
An infinite for loop runs indefinitely because it has no terminating condition.
5. Nested For Loop
A nested for loop is a loop inside another loop. It is used for multidimensional data or when multiple levels of iteration are needed.
1 2 3 2 4 6 3 6 9
6. For Loop with Step/Stride
Some programming languages allow you to specify a step or stride for the loop, letting you control the increment or decrement of the loop variable.
0 2 4 6 8