Open In App

Types of For loop in Programming

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

For Loop is a control flow statement that allows you to repeatedly execute a block of code based on a condition. It typically consists of an initialization, a condition, and an iteration statement.

Types of For loop in C:

In C, there is only one type of for loop, It consists of three main parts initialization, condition, and update.

Syntax:

for (initialization; condition; update) {
    // code block to be executed
}

Example:

C
#include <stdio.h>

int main() {
    for (int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Output
0 1 2 3 4 

Types of For loop in C++:

In C++, there are two types of for loop:

  1. Traditional For Loop
  2. Range-based For Loop

1. Traditional For Loop in C++:

Traditional For loop consists of three main parts initialization, condition, and update.

Syntax:

for (initialization; condition; update) {
    // code block to be executed
}

Example:

C++
#include <iostream>
using namespace std;
int main() {
      // Standard For loop
    for (int i = 0; i < 5; i++) {
        cout << i << "\n";
    }
    return 0;
}

Output
0 1 2 3 4 

2. Range-based For Loop in C++:

Range-based For Loop executes a for loop over a range. This type of loop is used to iterate over the values of a data structure, say array, vector, map, etc.

Syntax:

for (range_declaration : range_expression ) {

// code block to be executed

}

range_declaration: a declaration of a named variable, whose type is the type of the element of the sequence represented by range_expression, or a reference to that type. Often uses the auto specifier for automatic type deduction.

range_expression: any expression that represents a suitable sequence or a braced-init-list.

loop_statement: any statement, typically a compound statement, which is the body of the loop.

Example:

C++
#include <iostream>
using namespace std;
int main() {
      int arr[] = {10, 20, 30, 40, 50};
      // Range-based For loop
      for(int ele: arr)
          cout << ele << "\n";
    return 0;
}

Output
10 20 30 40 50 

Types of For loop in Java:

Java provides two types of for loops:

  1. Traditional For loop in Java
  2. For-each loop in Java

1. Traditional for loop in Java

The traditional for loop in Java is used to iterate over a block of code a specific number of times. It consists of three parts Initialization, Condition and Update.

Syntax:

for (initialization; condition; update) {
    // code block to be executed
}

Example:

Java
public class Main {
    public static void main(String[] args) {
        // Traditional for loop
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
    }
}

Output
0
1
2
3
4

2. For-each loop in Java

The for-each loop, also known as the enhanced for loop. It provides a simpler way to iterate over arrays or collections in Java.

Syntax:

for (type variableName : arrayName) {
    // code block to be executed
}

Example:

Java
public class Main {
    public static void main(String[] args) {
        // For-each loop (Enhanced for loop)
        int[] numbers = {10, 20, 30, 40, 50};
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}

Output
1
2
3
4
5

Types of For loop in Python:

In Python, there is primarily one type of for loop, which is used to iterate over sequences such as lists, tuples, strings, or other iterable objects. However, Python’s for loop is quite flexible and can be used in various ways:

for loop for iterating over a sequence: This is the most common use case for a for loop in Python, where it iterates over the elements of a sequence.

Example:

Python
# Iterate over a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

Output
apple
banana
cherry

for loop with range(): The range() function is often used with a for loop to iterate over a sequence of numbers.

Example:

Python
for i in range(5):
    print(i)

Output
0
1
2
3
4

Types of For loop in C#:

C# provides two types of for loops:

  1. Traditional for loop
  2. for-each loop

1. Traditional for loop in C#:

Traditional for loop in C# is Similar to C, C++ for loop.

Syntax:

for (initialization; condition; increment)
{
    // code to be executed
}

Example:

C#
using System;

class Program
{
    static void Main()
    {
        // Traditional For Loop
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(i);
        }
    }
}

Output
0
1
2
3
4

2. For-each Loop in C#:

The foreach loop in C# is used for iterating over elements in arrays and collections. It simplifies the process of iterating over such structures compared to the traditional for loop.

Example:

C#
using System;

class Program
{
    static void Main()
    {
        // Foreach Loop
        int[] numbers = { 1, 2, 3, 4, 5 };
        foreach (var number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

Output
1
2
3
4
5

Types of For loop in JavaScript:

In JavaScript, there are three types of for loops:

  1. Traditional for loop
  2. for…in loop
  3. for…of loop

1. Traditional for loop in JavaScript:

Traditional for loop is used to iterate over a block of code a fixed number of times. It’s particularly useful when you know how many times you want to loop beforehand.

Syntax:

for (initialization; condition; increment/decrement) {
    // code block to be executed
}

Example:

JavaScript
for (let i = 0; i < 5; i++) {
    console.log(i);
}

Output
0
1
2
3
4

2. for…in loop in JavaScript:

The for…in loop is used to iterate over the properties of an object. It enumerates the properties of an object in arbitrary order.

Syntax:

for (variable in object) {
    // code block to be executed
}

Example:

JavaScript
const person = { name: 'Sandeep', age: 30, city: 'Noida' };
for (const key in person) {
    console.log(key + ': ' + person[key]);
}

Output
name: Sandeep
age: 30
city: Noida

3. for…of loop in JavaScript:

The for…of loop in JavaScript is used to iterate over the values of an iterable object, such as an array, string, map, set.

Syntax:

for (variable of iterable) {
    // code block to be executed
}

Example:

JavaScript
const countries = ['India', 'China', 'Japan'];
for (const country of countries) {
    console.log(country);
}

Output
India
China
Japan

There are many types of For loop in different languages but all of serve the same purpose, that is to execute a block of code repeatedly. These are some of the common variations of for loops, each serving different purposes depending on the specific requirements of the program.




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads