SEARCH THIS BLOG

while LOOP IN C/C++

What is while loop in C/C++?

The while loop in C/C++ is a flow of control construct which allows the programmer to repeat a set of statements any desired number of times. Such programming constructs are really useful as instead of writing the same statement n number of times we can simply write it once and repeat it n times.

Suppose you have to print all numbers from 1 to n, where n is to be entered by the user. This n can be in thousands and even lacs. It would be impossible for a person to write thousand cout statements. In such cases he can simply write one cout statement and ask the compiler to repeat it n number of times. The while loop is one of the tools of telling the compiler to repeat the statement.

Let us learn in details about while loop in C/C++ :

General Syntax :

initialization;

while (condition)

{

    // body of the loop

     update;

}

Let us read about the working of while loop.

How does while loop work?

As we have seen in the syntax, there are four segments associated with any loop. They are initialization, condition, update statement and the body of the loop. Let us learn about them :

Initialization : It is the first step in while loop. This step initializes the loop variable with an initial value. A loop variable is the variable on which the condition is checked before executing the body of the loop.

Condition : After initialization, the condition is checked. Condition gives Boolean results, i.e. either true or false. If the condition is true, the control passes into the body of the loop and executes all the statements. If false, the control leaves the loop and comes out of it.

Body of the loop : It is the set of statements that is executed if the condition evaluates to true. It is basically the statement we wish to repeat.

Update : After the body of the loop is executed, the control moves to the update statement. An update statement changes the value of the loop variable. It is not mandatory to update the loop variable at last. This can be done anywhere inside the loop.

After update, the control again moves to condition and checks if it is still true. If yes, then the body is again executed. Otherwise, the control moves out of the loop.

The following flowchart helps you understand clearly the flow of control in a while loop :

 

while loop in C/C++

Let us visualize the flowchart with a demo code.

//The loop part to print all

//numbers from 1 to 99

int i = 1;

while (i < 100)

{

   cout<< i << “ ”;

   i++;

}

The equivalent flowchart :

while loop in C/C++

 

A demo program with detailed dry run 

//sum of odd numbers from 1 to n

#include<iostream>

using namespace std;

int main()

{

   int n;

   cin>>n;

   int sum=0;

   int i = 1;

   while(i <= n)

   {

       sum=sum+i;

       i = i + 2;

   }

   cout<< “Sum is ”<<sum;

   return 0;

}

Input : 10

Output : Sum is 25

Let us see how control flows in this program :

1. The program declares an integer variable n and then takes input from  the user.

2. Another variable named sum is created and initialized with 0 (zero). This variable stores the sum of numbers.

3. As the program is to find sum of odd numbers from 1 to n, we initialize the loop variable (i) with 1.

4. The condition is next checked, and if found true the control enters the body of the loop.

5. Inside the loop, the value of i is added to sum and the result is stored again into sum.

6. Then the control moves to update statement where value of  i is increased by 2 so that it becomes the next odd number.

7. Control again checks the condition and if found true, all steps from step 5 are again repeated.

8. If condition is false, the control moves out of the loop and prints the sum.

 

 

 

If you want to add any information or find something incorrect in this article, comment down or contact us through the mail : coding.nkcoder@gmail.com.  

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download pdf of this article here.


 


Post a Comment