Looping statements

Control Structure with Loops:


Loops are used to reduce your work, it generally helps us by not writing the stuff over and over when we want to do some type of work at a particular time.


There are mainly three types of loops for loop, while loop and do-while loop.


let's learn them one by one, firstly we are going to learn for loop then while loop and at last, we go through the do-while loop





For Loop :


Syntax:


for(initialization ; condition ; reinitialization ){


Statements to be run for this particular loop


}


Okay, Now here "initialization" means from where you want to start your loop


Condition means you need to specified condition here, in looping where the condition becomes false control is pass to the next statement and now we exited from a loop.


what if the condition becomes true, oh yes when the condition becomes true then loops statements will be executed and the process of reinitialization is done after that.


here the Reinitialization means do something with already initialized variable.


let's do something with for loop


Example:














Now let's talk about the while loop...


while loop:


Syntax:


initialization;


while(condition){


Statements to be run for this particular loop


Reinitialization


}


here initialization, Condition, and Reinitialization is the same as we saw previously.


let's   Direct deep drive into examples


Example:











Do while loop


the do-while loop is an exit control loop because the do-while loop executes once whether the condition specified id true or false and the condition is tested after the execution of body.


syntax 


do{


body of do-while loop


}while( condition );


note: don't forget to add a semicolon  ( ; ) at the end of the do-while loop.


do-while loop always use full when we want to execute our loop at once for example in any service when we call on helpline number then we might heart that for English press 1, for Hindi press 2, for Gujarati press 3, ... for repeat press 0, In ATM we also have a menu when we start so it is also an example of the do-while loop.


Example