Topic :While Loop in C++ Programming Language:



While Loop:

A while loop statement repeatedly executes a target statement as long as a given condition is true. in a while loop, a Boolean expression is evaluated. if the expression in the while statement is true, the resulting action, called Loop Body (which can be a single statement or a block of statements) executes, and the Boolean expression is tested again. if it is false, the loop is over, and program execution continues with the statement that follows the while statements. the cycle of execute-test-execute-test continues as long as the Boolean expression continues to be evaluated as true as shown in figure.



while Loop

Syntax:

The Syntax of the while-loop is:


while ( condition )
{
	/*Block of one ore more C++ statements to be 
	  execute if the condition is true. */
}



The Parentheses around "condition" are required. as long as "condition" is True, the block of one or more C++ statements executes repeatedly until Condition becomes False. Braces are required before and after the body of the while-loop unless we want to execute only one statement. Each statement in the body of the while-loop requires are ending semicolon.

The Placeholder "condition" usually contains relational, and possibly logical, Operators. These operators provide the True-False condition checked in "Condition" if "Condition" become false at starting of while -loop, the body of while-loop does not execute at all, because first "condition" will be tested, if condition is TRUE, the loop-body will be execute, else there is Nothing to execute in the loop-body if condition Become FALSE.



If the body of while-loop contains only one statement, the braces surrounding it are not required but it is good programming practice to enclose all while-loop statements in braces.


Example:

Let's have an simple example to understand how while-loop work. In this example, we will create a simple Game. we will hide a number here, and can ask to our friend to Guess the number. This loop will be execute repeatedly until your friend Guess the right Number.



/* Guessing Game: InfoBrother */
 
#include <iostream>
using namespace std;
main()
{
	int number=55; /*Need to be guess this Number:*/
	int guess;     /*User guess will store here.*/
 
	cout<<" Guess Number: ";
	cin>>guess;
 
	/*while guess is not equal to 55, execute loop-body again and again.
	  until loop-condition become false. mean Guess become equal to 55*/
 
	while (guess != number) 
	{ //body begin.
	  	/*the body of while-loop will execute, until user Guess right number.*/
    	cout<<" Wrong Guess...!!" <<endl;
    	cout<<" \nGuess again: ";
    	cin>>guess;
	} //body close.
 
 
	/*if user Guess right Number, then the condition of while-loop become false
	  and the loop will be over and the next statement will execute.*/
 
	cout<<" Congrats this is the right Guess. :"<<endl;
	return 0;
}


Example:

let's have another useful example: in this example, the program will ask to enter UpperLimit (any number ) and will add these all number till Upperlimit and give you the sum. For example: if you enter "5" as an upperLimit, then it will add 0+1+2+3+4+5 and give you the sum (i.e. 15). it will start from 0 and will end at upperLimit.



/* While-loop to getting sum.*/
 
#include<iostream>
using namespace std;
main()
{
	int UpperLimit; //to get Upper Limit from User.
	int sum=0;     //will used to store the Sum of Numbers.
	int Number=0;  //all Numbers less than upperlimits.
 
	cout<<" Enter the Upper Limit to get sum: ";
	cin>>UpperLimit;
 
	/*Loop will be continue until reach at UpperLimit Number.*/
	while(Number<UpperLimit) //Number is less than UpperLimit.
	{
		Number=Number+1; //Increment number one by one.
		sum=sum+Number; //All numbers will added to sum and will store in sum variable.
		//Example. sum=0+1=1; sum=1+1=2 and so on.
	}
 
	/*when condition went false, mean Number will reach upperlimit.*/
        cout<<"The sum of first "<<UpperLimit<<" Number are: "<<sum;
 
        return 0;
}


Infinite Loop:

Be careful while using Loop. by using wrong condition, we can face an infinite loop. always select an limit for the loop, else condition of the loop will always true and become an infinite loop. Here is an example, Copy and paste this code into your compiler and check How infinite loop work. in this Example Program will ask to Enter the Starting Number. you can put any Number. and Loop will become Infinite Loop. because the condition is " Number > 0 " Obviously all positive Numbers are greater than 0. so there is No limit to terminate the Program. so the condition will always true and program will always continue.



/*Example of while-infinite-loop:*/
 
#include<iostream>
using namespace std;
main()
{
    int Number=0; //to get starting Number from user.
 
    cout<<"please enter the starting Number: ";
    cin>>Number; 
 
    /*while Number is greater than zero. Mean there is no limit.*/
    while(Number>0) 
    {
        cout<<Number<<","; //print Number.
        ++Number;          //Increment Number.
    }
 
    return 0;
}


Any C++ expression can be placed inside the required parentheses in the while statement. As with the if statement, if the expression evaluates to false or zero, then the loop body is not entered. If the expression within the parentheses evaluates to true or any non-zero value, the loop body executes. With a while statement, when the expression is evaluated as true, the statements that follow execute repeatedly as long as the expression remains true.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement