Looping Statements in Programming

Looping statements, also known as iteration or repetition statements, are used in programming to repeatedly execute a block of code. They are essential for performing tasks such as iterating over elements in a list, reading data from a file, or executing a set of instructions a specific number of times. Here are some common types of looping statements:

The for loop is used to iterate over a sequence (e.g., a list, tuple, string, or range) and execute a block of code for each item in the sequence.

C++




#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i < 5; i++) {
        cout << i << endl;
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    for (int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}


Java




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


C#




using System;
 
class Program {
    static void Main(string[] args) {
        for (int i = 0; i < 5; i++) {
            Console.WriteLine(i);
        }
    }
}


Javascript




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


Python3




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


Output

0
1
2
3
4

The while loop is used to repeatedly execute a block of code as long as a specified condition is true.

C++




#include <iostream>
using namespace std;
int main()
{
    int count = 0;
    while (count < 5) {
        cout << count << endl;
        count++;
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int count = 0;
    while (count < 5) {
        printf("%d\n", count);
        count++;
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int count = 0;
        while (count < 5) {
            System.out.println(count);
            count++;
        }
    }
}


C#




using System;
 
class Program
{
    static void Main(string[] args)
    {
        int count = 0;
        while (count < 5)
        {
            Console.WriteLine(count);
            count++;
        }
    }
}


Javascript




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


Python3




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


Output

0
1
2
3
4

In some programming languages, such as C and Java, a do-while loop is used to execute a block of code at least once, and then repeatedly execute the block as long as a specified condition is true.

C++




#include <iostream>
using namespace std;
int main()
{
    int count = 0;
    do {
        cout << count << endl;
        count++;
    } while (count < 5);
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int count = 0;
    do {
        printf("%d\n", count);
        count++;
    } while (count < 5);
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int count = 0;
        do {
            System.out.println(count);
            count++;
        } while (count < 5);
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int count = 0;
        do {
            Console.WriteLine(count);
            count++;
        } while (count < 5);
    }
}


Javascript




let count = 0;
do {
    console.log(count); // Print the current value of count
    count++;         // Increment count by 1
} while (count < 5);     // Continue looping as long as count is less than 5


Output

0
1
2
3
4

4. Nested Loops in Programming:

Loops can be nested within one another to perform more complex iterations. For example, a for loop can be nested inside another for loop to create a two-dimensional iteration.

C++




#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "i=" << i << " j=" << j << "\n";
        }
    }
}


C




#include <stdio.h>
 
int main()
{
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("i=%d j=%d\n", i, j);
        }
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.println("i=" + i + " j=" + j);
            }
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                Console.WriteLine($"i={i} j={j}");
            }
        }
    }
}


Javascript




for (let i = 0; i < 2; i++) {
    for (let j = 0; j < 2; j++) {
        console.log(`i=${i} j=${j}`);
    }
}


Python3




for i in range(2):
    for j in range(2):
        print(f"i={i} j={j}")


Output

i=0 j=0
i=0 j=1
i=1 j=0
i=1 j=1

Each programming language may have its own syntax and specific variations of these looping statements.

Control flow statements in Programming

Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.

Table of Content

  • What are Control Flow Statements in Programming?
  • Types of Control Flow statements in Programming
  • Conditional Statements in Programming
  • Looping Statements in Programming
  • Jump Statements in Programming

Similar Reads

What are Control Flow Statements in Programming?

Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program. They enable execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of code, etc....

Types of Control Flow statements in Programming:

Control Flow Statements Type Control Flow Statement Description Conditional Statements if-else Executes a block of code if a specified condition is true, and another block if the condition is false. switch-case Evaluates a variable or expression and executes code based on matching cases. Looping Statements for Executes a block of code a specified number of times, typically iterating over a range of values. while Executes a block of code as long as a specified condition is true. do-while Executes a block of code once and then repeats the execution as long as a specified condition is true. Jump Statements break Terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch. continue Skips the current iteration of a loop and continues with the next iteration. return Exits a function and returns a value to the caller. goto Transfers control to a labeled statement within the same function. (Note: goto is generally discouraged due to its potential for creating unreadable and error-prone code.)...

Conditional Statements in Programming:

Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:...

Looping Statements in Programming:

...

Jump Statements in Programming:

...

Contact Us