loop
loop
#include <stdio.h>
Int main()
Return 0;
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-
controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated
at the end of the loop body. The loop body will execute at least once,
irrespective of whether the condition is true or false. do-while Loop is Exit
Controlled loop.
Loops in c
For loop first Initializes, then condition check, then executes the body and
at last, the update is done.
While loop first Initializes, then condition checks, and then executes the
body, and updating can be inside the body.
Do-while loop do-while first executes the body and then the condition
check is done.
For Loop
Syntax:
//
//
Example:
In for loop, a loop variable is used to control the loop. Firstly we initialize the
loop variable with some value, then check its test condition. If the statement
is true then control will move to the body and the body of for loop will be
executed. Steps will be repeated till the exit condition becomes true. If the
test condition will be false then it will stop.
Update Expression: After execution of the loop body loop variable is updated
by some value it could be incremented, decremented, multiplied, or divided
by any value.
For loop in c
Example:
#include <stdio.h>
// Driver code
Int main()
Int I = 0;
Return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
While Loop
While loop does not depend upon the number of iterations. In for loop the
number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition. If the test
condition will become false then it will break from the while loop else body
will be executed.
Syntax:
Initialization_expression;
While (test_expression)
Update_expression;
}
While loop in C
// C program to illustrate
// while loop
#include <stdio.h>
// Driver code
Int main()
// Initialization expression
Int I = 2;
// Test expression
// loop body
// update expression
I++;
}
Return 0;
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Do-while Loop
The do-while loop is similar to a while loop but the only difference lies in the
do-while loop test condition which is tested at the end of the body. In the do-
while loop, the loop body will execute at least once irrespective of the test
condition.
Syntax:
Initialization_expression;
Do
Update_expression;
} while (test_expression);
Do while loop in C
// C program to illustrate
// do-while loop
#include <stdio.h>
// Driver code
Int main()
// Initialization expression
Int I = 2;
Do
// loop body
// Update expression
I++;
// Test expression
Return 0;
}
Output
Hello World
Above program will evaluate (i<1) as false since I = 2. But still, as it is a do-
while loop the body will be executed once.
NameDescription
Break statement the break statement is used to terminate the switch and
loop statement. It transfers the execution to the statement immediately
following the loop or switch.
Infinite Loop
An infinite loop is executed when the test expression never becomes false
and the body of the loop is executed repeatedly. A program is stuck in an
Infinite loop when the condition is always true. Mostly this is an error that
can be resolved by using Loop Control statements.
#include <stdio.h>
// Driver code
Int main ()
Int I;
// is blank
For ( ; ; )
Return 0;
Output
// loop
#include <stdio.h>
// Driver code
Int main()
While (1)
Return 0;
Output
// C program to demonstrate
#include <stdio.h>
// Driver code
Int main()
Do
} while (1);
Return 0;
Output