0% found this document useful (0 votes)
7 views11 pages

Loops

The document discusses loops in C programming, explaining their purpose to repeat code until a condition is met. It describes two types of loops: entry-controlled (for and while loops) and exit-controlled (do-while loop), along with their syntax and behavior. Additionally, it covers loop control statements and the concept of infinite loops, which occur when the test condition never becomes false.

Uploaded by

ouanoaizel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Loops

The document discusses loops in C programming, explaining their purpose to repeat code until a condition is met. It describes two types of loops: entry-controlled (for and while loops) and exit-controlled (do-while loop), along with their syntax and behavior. Additionally, it covers loop control statements and the concept of infinite loops, which occur when the test condition never becomes false.

Uploaded by

ouanoaizel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:

You might also like