Open In App

While loop Syntax

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

While-Loop-GeeksforGeeks

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:

  • In C and C++, the while loop executes a block of code repeatedly as long as the specified condition evaluates to true.
  • The loop first evaluates the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

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

C++




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


C




#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:

  • In Java, the while loop operates similarly to C and C++.
  • It first evaluates the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of While loop Syntax in Java:

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:

  • Python’s while loop follows a similar structure to other languages.
  • It begins by evaluating the condition specified after the while keyword.
  • If the condition is true, the code block indented below is executed.
  • After executing the code block, the loop returns to the while statement and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop ends.

Implementation of While loop Syntax in Python:

Python3




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:

  • In C#, the while loop behaves similarly to C, C++, and Java.
  • It starts by evaluating the condition specified within the parentheses ().
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of While loop Syntax in C#:

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:

  • JavaScript’s while loop follows the same pattern as other languages.
  • It evaluates the condition specified within the parentheses () before each iteration.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop returns to the beginning and re-evaluates the condition.
  • If the condition remains true, the loop continues; otherwise, it ends.

Implementation of While loop Syntax in JavaScript:

Javascript




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


Output

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 



Similar Reads

Difference between While Loop and Do While Loop in Programming
While loop and Do while loop concepts are fundamental to control flow in programming, allowing developers to create loops that repeat a block of code based on certain conditions. The choice between "while" and "do while" depends on the specific requirements of the program and the desired behavior of the loop. It is important for a beginner to know
5 min read
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. Table of Content Do While loop Syntax in C/C++Java Do While loop SyntaxDo While loop
5 min read
Difference between For, While and Do-While Loop in Programming
For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true. A Do-While loop runs at least once and then continues if a condition is true. For Loop in Programming:The for loop is used when you know in advance how many times yo
5 min read
Difference Between For Loop and Do while Loop in Programming
For loop and Do while loop are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. For Loop in Programming:The for loop is used when you know in advance how many times you want
4 min read
Difference between For Loop and While Loop in Programming
Both for loops and while loops are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. [caption width="800"]Difference between For Loop and While Loop[/caption]For Loop in Prog
3 min read
For loop Syntax
For loop is a control flow statement in programming that allows you to execute a block of code repeatedly based on a specified condition. It is commonly used when you know how many times you want to execute a block of code. [caption width="800"]For loop Syntax[/caption] Table of Content For loop Syntax in C/C++For loop Syntax in JavaFor loop Syntax
5 min read
While loop in Programming
While loop is a fundamental control flow structure in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. While loop works by repeatedly executing a block of code as long as a specified condition remains true. It evaluates the condition before each iteration, executes the code block if th
11 min read
Do-While loop in Programming
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. In this article, we will learn about the basics of Do while loop, its syntax and its
10 min read
What is a Syntax Error and How to Solve it?
Syntax error is an error in the syntax of a sequence of characters that is intended to be written in a particular programming language. It’s like a grammatical error in a programming language. These errors occur when the code does not conform to the rules and grammar of the language. Syntax errors are detected at compile-time in compiled languages,
7 min read
What is Syntax? Components, Rules, and Common Mistakes
Programming languages serve as the foundation for building software applications, enabling developers to communicate instructions to computers effectively. A critical aspect of programming is understanding and implementing syntax and structure. In this article, we'll delve into the significance of programming syntax, its components, and best practi
10 min read
Article Tags :