For Loop
A Loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of Times.
Syntax :
for ( init ; condition ; increment ) {
statements(s);
}
The init step is excited first , and only once . This step allows you to declare and initialize any loop any loop control variables . you are not required to put a statements here , as a long as a semicolon.
Next , the Condition is evaluated . if it is true , the body of the loop is executed . if it is false , the body of the loop does not execute and flow of control jump to the next statement.
After the body of the loop executed , the flow of control jumps back up the increment statements . This statements can be left blank , as long as a semicolon appears after the condition
The condition is now evaluated again . if it is true , the loop executes and the process repeat itself ( body of the loop , then increment step , and then again condition ). After the condition becomes false , the for loop terminates .
Example :
Output :
1 2 3 4 5
In this loop , first the condition is evaluated first and if return true then the statements inside while loop executed , this happen repeatedly until the condition return false . When condition return false , the control comes out of the loop and jumps to the next statements in the program after while loop .
Note : The important point in while-loop we use the Increment and Decrement operator statements inside the loop so that the loop variables gets changed on each iteration , and at some points condition return false.
Output :
1 2 3 4 5
Infinite while Loop
The loop which is never stop is said that to be infinite while loop , when we give a condition which is never return false , then the loop become infinite and repeat itself.
Do-while is used to iterate a part of the program several times . if the number of iteration is not fixed and you and you must have to execute the loop at least once , it is recommended to use do-while loop.
The C++ do-while executed at least once because condition is checked after loop body
Output :
1 2 3 4 5
0 Comments
Do not Comment and any spam text