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.
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
public class ForLoopExample {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}
for i in range(1, 6):
print(i, end=" ")
print()
using System;
class Program {
static void Main() {
for (int i = 1; i <= 5; i++) {
Console.Write(i + " ");
}
Console.WriteLine();
}
}
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.
#include <iostream>
using namespace std;
int main() {
for(int i = 2; i <= 10; i+=2) {
cout << i << " ";
}
}
#include <stdio.h>
int main() {
for(int i = 2; i <= 10; i+=2) {
printf("%d ", i);
}
return 0;
}
public class GFG {
public static void main(String[] args) {
for(int i = 2; i <= 10; i+=2) {
System.out.print(i + " ");
}
}
}
for i in range(2, 11, 2):
print(i, end=" ")
using System;
class GFG {
static void Main() {
for(int i = 2; i <= 10; i+=2) {
Console.Write(i + " ");
}
}
}
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.
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
for(int num : numbers) {
cout << num << " ";
}
}
public class GFG {
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
for(int num : numbers) {
System.out.print(num+" ");
}
}
}
numbers = [1,2,3,4,5]
for num in numbers:
print(num, end=" ")
using System;
class GFG {
static void Main() {
int[] numbers = {1,2,3,4,5};
foreach(int num in numbers) {
Console.Write(num + " ");
}
}
}
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.
#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;
}
}
#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;
}
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);
}
}
}
i, j = 0, 10
while i < 5 and j > 0:
print("i=", i, ", j=", j)
i += 1
j -= 1
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);
}
}
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.
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.
#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;
}
#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;
}
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();
}
}
}
for i in range(1, 4):
for j in range(1, 4):
print(i * j, end=" ")
print()
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();
}
}
}
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.
#include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 10; i += 2) {
cout << i << " ";
}
}
#include <stdio.h>
int main() {
for(int i = 0; i < 10; i += 2) {
printf("%d ", i);
}
return 0;
}
public class GFG {
public static void main(String[] args) {
for(int i = 0; i < 10; i += 2) {
System.out.print(i + " ");
}
}
}
for i in range(0, 10, 2):
print(i, end=" ")
using System;
class GFG {
static void Main() {
for(int i = 0; i < 10; i += 2) {
Console.Write(i + " ");
}
}
}
for(let i = 0; i < 10; i += 2){
process.stdout.write(i + " ");
}
Output
0 2 4 6 8