Introduction
Aloopis a sequence of instruction s that is continually
repeated until a certain condition is reached.
Loops type in c
The While statement
Awhileloop in C programming repeatedly executes a target
statement as long as a given condition is true.
Syntax: while(condition)
{
statement(s);
}
Statement(s)may be a single statement or a block of
statements. Theconditionmay be any expression, and true
is any nonzero value. The loop iterates while the condition is
true.
When the condition becomes false, the program control
passes to the line immediately following the loop.
While: Flow Diagram
Infinite Loop Using while
While loop won’t terminate if the controlling expression
always has a nonzero value
Eg: while(1)
Will execute forever unless its body contains a statement
that transfers control out of loop (break, goto, return)
The do… while statement
do.... While is essentially just a while statement whose
controlling expression is tested after each execution of
the loop body
Syntax
do{
statements
}
while (expression);
for statement
Aforloop 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/decrement
{
statements;
}
Infinite Loop using for
Like while infinite loop can be created using for
Syntax(or code): for(;;)
Exiting from loop
The break statement
Example: Program to check
whether a number
is prime or not