Before we use Do while loop statement we must Understand the Do While loop:
The do while loop is similar to the while loop with one important difference. In while Loop first check the condition and then execute the statements. But in Do while loop first loop execute the statements and then check the condition and decide that where the control transfer ( for example again to loop or after the loop ). Hence, the do...while loop is executed at least once.
Now if we want to print a message " Hello Learning Hints " 5 time on screen then we set a counter variable which check the number of execution and then check this counter in while loop if total number of execution are 5 then loop will be terminated.
Syntax of do while loop :
do
{
(statements) ;
}
while (test Expression);
For Example
int counter=6 ;
do
{
printf("Hello Learning Hints \n") ;
}while (counter <=5)
Result
Hello Learning Hints
In this above example we se that before while, loop execute the statement and then check the condition as well counter variable is already grater then 5 so loop will be terminated and control is transfer to next statement after the loop.
Where Test Expression is an condition (relational expression) which check that when loop will terminate. when condition becomes false loop will be terminated.
for example while (counter <=5)
Note:- (Relational expression compare two variable or constant and give the result in shape of true or false for example 10 >12 return False)
after initialization of loop for example while (counter <=5) every thing in braces or "curly brackets " { " and " } " is called body of loop will be executed the number of time of the loop.
Example :1 Display 1-10 number on the screen
#include int main() { int counter =1; do { printf("%d \t ",counter); counter=counter+1; } while (counter <=10) ; return 0; } Result 1 2 3 4 5
Explanation of above example:
int counter =1;
this is initialization and declaration of variable which tell to complier that counter is an integer variable and have a value 1.
do
{
This is start of the loop body
This process repeated again and again until counter have a value 5 and condition become false then loop will be terminated and control transfer after the body of loop.
printf("%d \t ",counter);
This statement just display the value of counter on the screen. %d tell to complier that counter have an integer value. \t dive a vertical Tab
counter ++;
This statment increase the value of counter one time
(counter=counter +1).
} while (counter <=10) ;
This is the end of the body and the point where loop check the condition and decide what do next ( again execute the statements in the body of loop or terminated the loop)
if the condition become False then loop will be terminated.
return 0;
This statement is the "Exit status" of the program. In simple terms, program ends.
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++