The While Loop

A While loop is also referred to as a pre-tested loop. A while loop in a computer allows a piece of code to be executed several times depending on a given test condition that evaluates to true or false. The while loop is typically used when the number of iterations is unknown. If the number of iterations is known, we may alternatively utilise the previously mentioned for loop.

The syntax for utilising a while loop is as follows.

while (condition test)
{
    // Set of statements
}

A while loop’s body can include a single statement or a block of statements. The test condition might be any expression that should return true or false. While the test condition is true, the loop iterates. It finishes when the condition turns false.

This is one illustration of how a while loop works.

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

Output:

5 6 7 8 9

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in