C++ 10: Loops - While, For,DoWhile

Loops are used to repeatedly execute a block of code. C++ provides three different loping structures as follows.
  1. While Loop
  2. For Loop
  3. Do while Loop

While Loop:
Statements within this loop are repeatedly executed while the specified condition is true. The program control exit from the loop when specified condition becomes false.
Syntax:
    while(condition)
    {
                .......
                .......
    }

Here, frist the given condition is checked. If the condition is true then the program control enter inside the loop and executes the code present in that loop. when it reaches at the end of loop, the program control is again move at the starting of the loop to check the condition is true then the above process repeats. if the condition is false then the program control exits from the loop.

Program:
#include <iostream>
using namespace std;
main()
{
    int i = 1;

    while (i <= 5)
    {
        cout << "Ayush ";
        i++;
    }
    return 0;
}

Output

Ayush Ayush Ayush Ayush Ayush

Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected