Loop – for, while, do-while
Md. Mushfiqul Haque
Lecturer, UIU
C – loops
Loops are used to repeat a block of code until the specified condition is met.
Entry controlled loop
In Entry controlled loops the test condition is checked before entering the
main body of the loop. For Loop and While Loop are Entry-controlled loops.
for Loop
for loop in C programming is a repetition
control structure that allows programmers to
write a loop that will be executed a specific
number of times.
• 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 until the test
condition is false.
for Loop
• Initialization Expression: In this expression, we assign a loop variable or loop counter to
some value. for example: int i=1;
• Test Expression: In this expression, test conditions are performed. If the condition evaluates
to true then the loop body will be executed and then an update of the loop variable is done.
If the test expression becomes false then the control will exit from the loop. for example,
i<=9;
• 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.
Syntax: Example:
for loop
while loop
• In the While loop, the execution
is terminated on the basis of the
test condition. If the test
condition becomes false then it
breaks from the while loop else
body is executed.
while loop
while loop
Exit controlled loop
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.
do-while
• 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.
do-while
do-while
do-while
Practice
Practice
Practice
Practice
Practice
Practice
break statement
The break in C is a loop control statement that breaks out of the loop
when encountered.
It can be used inside loops or switch statements to bring the control out
of the block.
The break statement can only break out of a single loop at a time.
Syntax of break in C:
break;
Example
We can use it with any type of
loop to bring the program
control out of the loop.
Output:
continue Statement
• The C continue statement resets program control to the beginning of
the loop when encountered.
• As a result, the current iteration of the loop gets skipped and the
control moves on to the next iteration.
• Statements after the continue statement in the loop are not executed.
• Syntax of continue in C: The syntax of continue is just the continue
keyword placed wherever we want in the loop body.
continue;
Example
Output:
Nested Loop [Ref: [Link]
• A nested loop means a loop statement inside another loop statement.
• That is why nested loops are also called “loop inside loops“.
• We can define any number of loops inside another loop.
Nested for Loop
Nested for loop refers to any
type of loop that is defined
inside a ‘for’ loop. Below is
the equivalent flow diagram
for nested ‘for’ loops:
Nested for loop
Example
Nested While Loop
A nested while loop refers to
any type of loop that is defined
inside a ‘while’ loop.
Example
Nested do while loop
A nested do-while loop refers to any type of loop
that is defined inside a do-while loop.
Example
for-while-do while
Example
Break inside a nested loop
Whenever we use a break
statement inside the nested
loops it breaks the innermost
loop and program control is
inside the outer loop.
Example
Continue inside a nested loop
Whenever we use a continue statement
inside the nested loops it skips the
iteration of the innermost loop only.
The outer loop remains unaffected.
Example
Practice
Replace the “outer” for loop
using “while” loop and the
“inner” for loop using “do while”
loop in the following code
without changing the logical
meaning of the program.
Practice
Replace the “outer” for loop using
“while” loop and the “inner” for
loop using “do while” loop in the
following code without changing
the logical meaning of the program.
Practice
Practice