Loop Statements
Loop Statements
Fardin Saad
Lecturer,
Department of Computer Science
and Technology (CSE)
Loop Statements
In any programming language including C, loops are used to execute a set of statements repeatedly until a
particular condition is satisfied.
It mainly consists of 3 components:
Variable Initialization
Condition
Variable increment/decrement
Working procedure of a loop:
Types of Loops
Depending upon the position of a condition in a program, looping in C is classified into two types:
Entry Controlled Loop: A condition is checked before executing the body of a loop. It is also called as a pre-
checking loop.
Exit Controlled Loop: A condition is checked after executing the body of a loop. It is also called as a post-checking
loop.
Generally there are 3 types of loops in C:
while loop: Entry controlled
for loop: Entry controlled
do while loop: Exit controlled
while loop
while loop can be addressed as an entry control loop. It is completed in 3 steps
variable initialization
do
{
statements
variable increment/decrement
}
while(condition)
do while loop example
int main()
{
int x;
x = 1;
do
{
printf("%d ", x);
x++;
}
while(x <= 10)
}
Jumping Out of Loops
Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon
as a certain condition becomes true. This is known as jumping out of loop.
Break statement: When break statement is encountered inside a loop, the loop is immediately exited and the
program continues with the statement immediately following the loop.
Jumping Out of Loops
Continue statement: It causes the control to go directly to the test-condition and then continue the loop process.
On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.
For the for loop, continue statement causes the conditional test and increment portions of the loop to execute.
For the while and do...while loops, continue statement causes the program control to pass to the conditional tests .
THANK YOU