Many a times we require to repeat the code in C for that purpose we use the Loops in C.
Types of Loops
1) for
2) while
3) do-while
1. for Loop
One of the most used loop is for loop, and the loop is repeated till test expression is true. After every processing of the loop update expression is called. (all three expression can be null)
for(initial expression; test expression; update expression)
{
body of loop;
}
2. while Loop
While loop is nothing rewriting of the same loop and is executed will test expression is true.
initial expression; // optional
while(test expression)
{
body of loop;
update expression; // optional
}
3. do-while loop
DO..WHILE loops are useful for things that want to loop at least once.
do {
body of loop;
} while ( test expression );
test expression is tested at the end of the block instead of the beginning, so the block will be executed at least once. If test expression is true, we jump back to the beginning of the block and execute it again.
Continue and Break of Loop
Continue is used to stop the current iteration of the loop and control is shifted to the start of the loop for the next iteration.
Break is used to break the loop and control reaches outside of the loop as shown in the figure.