Open In App

While loop Syntax

While loop is a fundamental control flow structure (or loop statement) in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. Unlike the for loop, which is tailored for iterating a fixed number of times, the while loop excels in scenarios where the number of iterations is uncertain or dependent on dynamic conditions.



The syntax of While loop varies slightly depending on the programming language, but the basic structure is similar across many languages.



while (condition) {
    // Code block to be executed repeatedly as long as the condition is true
}

Here’s a general overview of the Syntax of While loop:

  1. Condition: This is the condition that is evaluated before each iteration of the loop. If the condition is true, the loop body is executed; otherwise, the loop terminates.
  2. Loop Body: This is the block of code that gets executed if the condition is true.
  3. Update: Inside the loop body, there should be some logic to change the variables involved in the condition so that the loop eventually terminates. Otherwise, you risk creating an infinite loop.

While loop Syntax in C/C++:

Syntax:

while (condition) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

Implementation of While loop Syntax in C/C++:




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




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

Output
0 1 2 3 4 5 6 7 8 9 

Java While loop Syntax:

Syntax:

while (condition) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

Implementation of While loop Syntax in Java:




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

Output
0 1 2 3 4 5 6 7 8 9 

While loop Syntax in Python:

Syntax:

while condition:
    # Code block to be executed repeatedly as long as the condition is true

Explanation of the Syntax:

Implementation of 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 

While loop Syntax in C#:

Syntax:

while (condition) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

Implementation of While loop Syntax in C#:




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

Output
0 1 2 3 4 5 6 7 8 9 

While loop Syntax in JavaScript:

Syntax:

while (condition) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

Implementation of While loop Syntax in JavaScript:




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

Output
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 


Article Tags :