Iteration Statements in Programming

Iteration statements, commonly known as loops, are statements in programming used to execute part of code repeatedly based on condition or set of conditions. These constructs are important for performing repetitive tasks efficiently. In this article, we will discuss various types of iteration statements and their use in different programming languages.

Types of Iteration Statements in programming:

There are mainly three types of iteration statements:

1. For Loop:

For loops iterate over a range of values or elements in a collection. These are mainly used where the number of iterations is known beforehand like iterating through elements in an array or predefined range.

Syntax

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

2. While Loops

While loops execute as long as specifies condition evaluates to true. These are suitable for situations where the number of iterations is not known and depends on dynamic conditions.

Syntax

count = 0
while count < 5:
    print(count)
    count += 1

3. Do-While Loops

Do while loops are similar to while loop but guarantee at least one execution of the loop body before checking the condition. These are useful when loop must execute at least once regardless of the condition.

Syntax

int count = 0;
do {
    cout << count << endl;
    count++;
} while (count < 5);

Iteration Statements across Different Languages:

Here, we will see the implementation of different iteration statements in different programming languages.

1. Iteration Statements in C:

C supports all the three loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in C:

C
#include <stdio.h>

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

    // While Loop
    int count = 0;
    while (count < 5) {
        printf("%d ", count);
        count++;
    }
    printf("\n");

    // Do-While Loop
    count = 0;
    do {
        printf("%d ", count);
        count++;
    } while (count < 5);
    printf("\n");

    return 0;
}

Output
0 1 2 3 4 
0 1 2 3 4 
0 1 2 3 4 

2. Iteration Statements in C++

C++ provides support for all the common loops like: for, while, and do-while loops. Below is the implementation of all types of iteration statements in C++:

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

int main()
{

    // For Loop
    for (int i = 0; i < 5; i++) {
        cout << i << " ";
    }
    cout << endl;

    // While Loop
    int count = 0;
    while (count < 5) {
        cout << count << " ";
        count++;
    }
    cout << endl;

    // Do-While Loop
    count = 0;
    do {
        cout << count << " ";
        count++;
    } while (count < 5);
    cout << endl;

    return 0;
}

Output
0 1 2 3 4 
0 1 2 3 4 
0 1 2 3 4 

3. Iteration Statements in Python:

Python supports for and while loops. It does not have a native do-while loop. But same thing can be achieved using while loop with a break condition. Below is the implementation of all types of iteration statements in Python:

Python
# For Loop
for i in range(5):
    print(i, end=" ")

print()

# While Loop
count = 0
while count < 5:
    print(count, end=" ")
    count += 1
print()

count = 0
while True:
    print(count, end = " ")
    count += 1
    if count >= 5:
        break

Output
0 1 2 3 4 
0 1 2 3 4 
0 1 2 3 4 

4. Iteration Statements in Java

Java provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in Java:

Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        // For Loop
        for (int i = 0; i < 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // While Loop
        int count = 0;
        while (count < 5) {
            System.out.print(count + " ");
            count++;
        }
        System.out.println();

        // Do-While Loop
        count = 0;
        do {
            System.out.print(count + " ");
            count++;
        } while (count < 5);
        System.out.println();
    }
}

Output
0 1 2 3 4 
0 1 2 3 4 
0 1 2 3 4 

5. Iteration Statements in C#:

C# provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in C#:

C#
using System;

public class GFG {

    static public void Main()
    {

        // For Loop
        for (int i = 0; i < 5; i++) {
            Console.Write(i + " ");
        }
        Console.WriteLine();

        // While Loop
        int count = 0;
        while (count < 5) {
            Console.Write(count + " ");
            count++;
        }
        Console.WriteLine();

        // Do-While Loop
        count = 0;
        do {
            Console.Write(count + " ");
            count++;
        } while (count < 5);
        Console.WriteLine();
    }
}

Output
0 1 2 3 4 
0 1 2 3 4 
0 1 2 3 4 

5. Iteration Statements in JavaScript:

JavaScript provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in JavaScript:

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

// While Loop
let count = 0;
while (count < 5) {
    console.log(count);
    count++;
}

// Do-While Loop
count = 0;
do {
    console.log(count);
    count++;
} while (count < 5);

Output
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4

Iteration statements, commonly known as loops, are fundamental constructs in programming that enable repetitive execution of code blocks based on specified conditions. Understanding and utilizing these loops effectively is crucial for implementing repetitive tasks efficiently, reducing code redundancy, and enhancing the overall clarity and maintainability of the code.



Contact Us