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.
C++
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 5; i++) {
        cout << i << " ";
    }
    cout << endl;
    return 0;
}
C
#include <stdio.h>

int main() {
    for(int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
Java
public class ForLoopExample {
    public static void main(String[] args) {
        for(int i = 1; i <= 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}
Python
for i in range(1, 6):
    print(i, end=" ")
print()
C#
using System;

class Program {
    static void Main() {
        for (int i = 1; i <= 5; i++) {
            Console.Write(i + " ");
        }
        Console.WriteLine();
    }
}
JavaScript
for(let i = 1; i <= 5; i++) {
    console.log(i);
}

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.

C++
#include <iostream>
using namespace std;

int main() {
    for(int i = 2; i <= 10; i+=2) {
        cout << i << " ";
    }
}
C
#include <stdio.h>

int main() {
    for(int i = 2; i <= 10; i+=2) {
        printf("%d ", i);
    }
    return 0;
}
Java
public class GFG {
    public static void main(String[] args) {
        for(int i = 2; i <= 10; i+=2) {
            System.out.print(i + " ");
        }
    }
}
Python
for i in range(2, 11, 2):
    print(i, end=" ")
C#
using System;

class GFG {
    static void Main() {
        for(int i = 2; i <= 10; i+=2) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
let numbers = [];
for(let i=2;i<=10;i+=2){
    process.stdout.write(i + " ");
}

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.

C++
#include <iostream>
using namespace std;

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    for(int num : numbers) {
        cout << num << " ";
    }
}
Java
public class GFG {
    public static void main(String[] args) {
        int[] numbers = {1,2,3,4,5};
        for(int num : numbers) {
            System.out.print(num+" ");
        }
    }
}
Python
numbers = [1,2,3,4,5]
for num in numbers:
    print(num, end=" ")
C#
using System;

class GFG {
    static void Main() {
        int[] numbers = {1,2,3,4,5};
        foreach(int num in numbers) {
            Console.Write(num + " "); 
        }
    }
}
JavaScript
let numbers = [1,2,3,4,5];
for(let num of numbers){
    process.stdout.write(num + " "); 
}

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.

C++
#include <iostream>
using namespace std;

int main() {
    for(int i=0, j=10; i<5 && j>0; i++, j--) {
        cout << "i=" << i << ", j=" << j << endl;
    }
}
C
#include <stdio.h>

int main() {
    for(int i=0, j=10; i<5 && j>0; i++, j--) {
        printf("i=%d, j=%d\n", i, j);
    }
    return 0;
}
Java
class GFG {
    public static void main(String[] args)
    {
        for (int i = 0, j = 10; i < 5 && j > 0; i++, j--) {
            System.out.println("i=" + i + ", j=" + j);
        }
    }
}
Python
i, j = 0, 10
while i < 5 and j > 0:
    print("i=", i, ", j=", j)
    i += 1
    j -= 1
C#
using System;

public class GFG {

    static public void Main()
    {

        for (int i = 0, j = 10; i < 5 && j > 0; i++, j--) {
            Console.WriteLine("i=" + i + ", j=" + j);
        }
    }
JavaScript
for(let i=0, j=10; i<5 && j>0; i++, j--){
    console.log("i=", i, ", j=", j);
}

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.

Java
for (;;) {
    // Infinite loop
}

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.

C++
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            cout << i * j << " ";
        }
        cout << endl;
    }
    return 0;
}
C
#include <stdio.h>

int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            printf("%d ", i * j);
        }
        printf("\n");
    }
    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        for(int i = 1; i <= 3; i++) {
            for(int j = 1; j <= 3; j++) {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}
Python
for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end=" ")
    print()
C#
using System;

class GFG {
    static void Main() {
        for(int i = 1; i <= 3; i++) {
            for(int j = 1; j <= 3; j++) {
                Console.Write(i * j + " ");
            }
            Console.WriteLine();
        }
    }
}
JavaScript
for(let i = 1; i <= 3; i++) {
    let row = "";
    for(let j = 1; j <= 3; j++) {
        row += (i * j) + " ";
    }
    console.log(row);
}

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.

C++
#include <iostream>
using namespace std;

int main() {
    for(int i = 0; i < 10; i += 2) {
        cout << i << " ";
    }
}
C
#include <stdio.h>

int main() {
    for(int i = 0; i < 10; i += 2) {
        printf("%d ", i);
    }
    return 0;
}
Java
public class GFG {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i += 2) {
            System.out.print(i + " ");
        }
    }
}
Python
for i in range(0, 10, 2):
    print(i, end=" ")
C#
using System;

class GFG {
    static void Main() {
        for(int i = 0; i < 10; i += 2) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
for(let i = 0; i < 10; i += 2){
    process.stdout.write(i + " "); 
}

Output
0 2 4 6 8 
Comment