Open In App

Do While loop Syntax

Do while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop’s body, ensuring that the loop’s body is executed at least once.



The syntax for a do while loop varies slightly depending on the programming language you’re using, but the general structure is similar across many languages.



do {
// Code block to be executed at least once
// This block will execute at least once
} while (condition);

Here’s a basic outline:

  1. Initialization: Initialize any loop control variables or other necessary variables before entering the loop.
  2. Do While Loop Structure: The loop consists of two main parts: the “do” block and the “while” condition.
    • The “do” block contains the code that will be executed at least once, regardless of the condition.
    • The “while” condition specifies the condition that must be true for the loop to continue iterating. If the condition evaluates to true, the loop will execute again. If it evaluates to false, the loop will terminate.
  3. Loop Execution: The loop executes the code inside the “do” block, then evaluates the “while” condition.
    • If the condition is true, the loop repeats, executing the code block again.
    • If the condition is false, the loop terminates, and control passes to the code following the loop.

Do While loop Syntax in C/C++:

Syntax:

do {
// Code block to be executed at least once
} while (condition);

Explanation of the Syntax:

Implementation of Do While Loop Syntax in C/C++:




#include <iostream>
using namespace std;
 
int main()
{
    int i = 0;
    do {
        cout << i << " ";
        i++;
    } while (i < 10);
    return 0;
}




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

Output
0 1 2 3 4 5 6 7 8 9 

Java Do While loop Syntax:

Syntax:

do {
// Code block to be executed at least once
} while (condition);

Explanation of the Syntax:

Implementation of Do While Loop Syntax in Java:




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int i = 0;
        do {
            System.out.print(i + " ");
            i++;
        } while (i < 10);
    }
}

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in Python:

Python doesn’t have a built-in do while loop, but a similar behavior can be achieved using a while loop with a condition check after the code block.

Syntax:

while True:
# Code block to be executed at least once
if not condition:
break

Explanation of the Syntax:

Implementation of Do While Loop Syntax in Python:




i = 0
while i < 10:
    print(i, end=" ")
    i += 1

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in C#:

Syntax:

do {
// Code block to be executed at least once
} while (condition);

Explanation of the Syntax:

Implementation of Do While Loop Syntax in C#:




using System;
 
public class GFG {
 
    static public void Main()
    {
        int i = 0;
        do {
            Console.Write(i + " ");
            i++;
        } while (i < 10);
    }
}

Output
0 1 2 3 4 5 6 7 8 9 

Do While loop Syntax in JavaScript:

Syntax:

do {
// Code block to be executed at least once
} while (condition);

Explanation of the Syntax:

Implementation of Do While Loop Syntax in JavaScript:




let i = 0;
do {
    console.log(i + " ");
    i++;
} while (i < 10);

Output
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

Article Tags :