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

Loop Statements

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

Loop Statements

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

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 #Program for printing first 10 numbers


while(condition) #include<stdio.h>
{
statements int main()
variable increment/decrement {
} int x;
x = 1;
while(x <= 10)
{
printf("%d ", x);
x++;
}
}
For loop
 for loop can also be addressed as an entry control loop.
 In for loop we have exactly two semicolons, one after initialization and second after the condition.
 In this loop we can have more than one initialization or increment/decrement, separated using
comma operator. But it can have only one condition.
 However multiple conditions can be imposed using logical operators

for(initialization; condition; increment/decrement)


{
statements


}
For loop examples (Nested)

#Program for printing first 10 numbers #Nested for loop


#include<stdio.h> #Program for printing first 10 numbers 5 times
#include<stdio.h>
int main()
{ int main()
int i; {
for(i = 1; i <= 10; i++) int i, j;
{ for(j = 1; j <= 5; j++)
printf("%d ", i); {
} for(i = 1; i <= 10; i++)
} {
printf("%d ", i);
}
printf(“\n”);
}
}
do while loop
 In some situations it is necessary to execute body of the loop before testing the condition.
 Such situations can be handled with the help of do-while loop which is an exit controlled loop.
 do statement evaluates the body of the loop first and at the end, the condition is checked using while statement.
 It means that the body of the loop will be executed at least once, even if the starting condition inside while is not
satisfied.

variable initialization
do
{
statements
variable increment/decrement
}
while(condition)
do while loop example

#Program for printing first 10 numbers


#include<stdio.h>

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

You might also like