VOOZH about

URL: https://www.geeksforgeeks.org/dsa/for-loop-in-programming/

⇱ For loop in Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

For loop in Programming

Last Updated : 26 Mar, 2026

A for loop allows you to repeat a block of code a specific number of times.

  • Widely used when the number of iterations is known in advance.
  • Makes it easy to iterate over arrays, lists, or sequences, generate number series, and perform repetitive tasks efficiently.

Output
1 2 3 4 5 

Flow of a For Loop

A for loop has three main parts in most languages: initialization, condition, and update.

  • Initialization sets the starting point of the loop.
  • Condition is checked before each iteration; if true, the loop executes.
  • Update changes the loop counter after each iteration.

In Python, a for loop is slightly different. It loops directly over an iterable and has two main parts: variable and iterable.

  • Variable holds the current element from the iterable during each iteration.
  • Iterable the collection of elements the loop goes through (like a list, tuple, string, or range).

Types of For Loops

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.


Output
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.


Output
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.


Output
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.


Output
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.


Output
0 2 4 6 8 
Comment
Article Tags:
Article Tags: