Programming's While Loop with example

 while Loop

The while loop is the simplest loop of C++ language. This loop executes one or more statements while the given condition remains true. It is useful when the number of iterations is not known in advance.

Programming's While Loop with example

Syntax

The syntax of while loop is as follows:

while (condition) ststatement;

while

It indicates the start of while loop.

condition

The condition is given as a relational expression. The statement is executed only if the given condition is true. The statement is never executed if the condition is false

statement

It represents the body of the loop. If the body of loop contains more than one statement, they are written in curly braces { }.

The syntax of while loop for a group of statements is as follows:

while (condition)

{

statement 1;

statement 2;

--------------

statement N;

}

Working of while Loop

The condition is evaluated first. The control enters the body of the loop and executes all statements if the condition is true. The control moves to the start of the loop and evaluates the condition again. This process continues as long as the condition remains true. The loop is terminated when the condition becomes false.

C++ Program that displays "Pakistan" for five times using while loop.


#include <iostream.h>

#include <conio.h>

void main ()

{

int n;

tions

n++;

clrser();

n = 1;

clrscr();

while(n<=5)

{

cout<<"Pakistan"<<endl;

n++;

}

getch();

}

Output:

Pakistan

Pakistan

Pakistan

Pakistan

Pakistan

How above Program Works?

The above program uses a variable n to control the iterations of the loop. It is known as counter variable. The variable n is initialized to 1. When the condition is evaluated for the first time, the value of n is less than 5 so the condition is true. The control enters the body of the loop. 

The body of loop contains two statements. The first statement prints Pakistan on the screen. The second statement increments the value of n by 1 and makes it 2. The control moves back to the condition after executing both statements. This process continues for five times. When the value of n becomes 6. the condition becomes false and the loop terminates.

C++ Program that inputs starting and ending number from the user and displays all even numbers in the given range using while loop.

 include <iostream.h>

#include <conio.h>

void main()

int n, s, e;

clrscr();

cout<<"Enter starting number";

cin>>s;

cout<<"Enter ending number";

cin>>e;

n=s;

while(n<=e)

{

if(n%2==0)

cout<<n<<endl;

n++;

}

getch():

}

Output:

Enter starting number: 1

Enter ending number: 10

2

4

6

8

10

How above Program Works?

The above program inputs two numbers from the user in s and e It uses a counter variable n to control the iterations of the loop. The value of s is assigned to variable n. The number of iterations depends on the values entered in s and e. For example, if the user enters 11 in s and 20 in e, loop will execute for ten times and program displays 12, 14, 16, 18 and 20.

C++ Program that uses a while loop to enter number from the user and then display it. The loop is terminated when the user enters-1.

#include <iostream.h>

#include <conio.h>

void main()

{

int n;

n = 1;

while(n != -1)

{

cout<<"Enter a number: ";

cin>>n;

cout<<"You entered "<<n<<endl;

}

cout<<"End of Program";

getch();

}

Output:

Enter a number: 5

You entered: 5

Enter a number: 10

You entered: 10

Enter a number: -1

You entered -1

End of Program...

How above Program Works?

The above example uses -1 to terminate the loop. The variable n is initialized to 1. First of all, the condition is evaluated and the control enters the body of the loop because the condition is true. 

The first statement in the body gets the input from the user and prints that value on the screen. Then the control moves the beginning of the loop and the condition is evaluated again. This process continues and the value entered by the user is displayed. The loop is terminated when the user enters -1.

Post a Comment

0 Comments