C Programming
Loops
Loops in C
• Loops in programming are used to repeat a block of code until the
specified condition is met. A loop statement allows programmers
to execute a statement or group of statements multiple times
without repetition of code.
Two Types of Loops in C
• 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.
Two Types of Loops in C
Loop Body
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. for loop enables programmers to perform n
number of steps together in a single line.
• Syntax:
Example
• 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.
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:
do-while Loop
• In the do-while loop, the loop body will execute at least once
irrespective of the test condition.
• Syntax:
Loop Control Statements
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.
• Example: