How the Loop statement works in C language

The looping statement is used for code to be run several times. The loop statement is exit when condition is over.A loop becomes an infinite loop if a condition never becomes false.
What are the Step to using loop statement
Step 1:Firstly to initialization happens and the counter variable gets initialized.
Step 2:The Second step the condition for checking, where the counter variable is tested for the given condition, if the condition returns true then the statements inside the body of loop gets executed, if the condition returns false then the loop gets exit and the control comes out of the loop.
Step 3: The successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –).

Flow chart of For loop
What are the types of Loop are used
- while loopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
- do…while loopIt is more like a while statement, except that it tests the condition at the end of the loop body.
- for loop
Most commonly used loop statement.Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
- nested loops
You can use one or more loops inside any other while, for, or do..while loop
while loop
The while loop allows a part of the code to be executed multiple times upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance.
Syntax
while (condition test) { //Statements to be executed repeatedly // Increment (++) or Decrement (--) Operation }
- The
while
loop evaluates the test expression inside the brackets()
. - If the test expression is true, statements inside the body of
while
loop are executed. Then, the test expression is evaluated again. - The process goes on until the test expression is evaluated to false.
- If the test expression is false, the loop terminates (ends).
do…while loop
The do..while loop is similar to the while loop with one important difference. The body of do…while loop is executed at least once. Only then, the test expression is evaluated.
The syntax of the do…while loop is:
do
{
// statements inside the body of the loop
}
while (testExpression);
- The body of do…while loop is executed once. Only then, the test expression is evaluated.
- If the test expression is true, the body of the loop is executed again and the test expression is evaluated.
- This process goes on until the test expression becomes false.
- If the test expression is false, the loop ends.
for Loop
The syntax of the for
loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
- The initialization statement will execute only once.
- Then, to test expression is evaluated. If the test expression is evaluated to false, the
for
loop is terminated. - However, if the test expression is evaluated to true, statements inside the body of
for
loop are executed, and the update expression is updated. - Again the test expression is evaluated.This process goes on until the test expression is false. When the test expression is false, the loop terminates.