Loops, break and
continue
19CSE102 Computer Programming
Lecture 3
C Iteration Statements (Loops)
A Loop executes the sequence of statements many times until the
stated condition becomes false.
it repeats the set of statements until the condition for termination is
met.
Iteration statements in C are
while
do-while
for
Loops
1. Entry controlled loop
In an entry-controlled loop, a condition is checked before executing
the body of a loop.
It is also called as a pre-checking loop.
2. Exit controlled loop
In an exit-controlled loop a condition is checked after executing
the body of a loop
It is also called as a post-checking loop.
Loops
While Loop in C
• The while loop evaluates the test expression inside the
parenthesis ().
• If the test expression is true, statements inside the body
of while loop are executed.
• The process goes on until the test expression is evaluated to
false.
• If the test expression is false, the loop terminates (ends).
While Flow Chart While Loop Syntax
While Loop Example 1
include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
While loop Example- 2
Examples of infinite while loop Example 1:
#include <stdio.h>
int main()
{
int var = 6; Infinite loop: var will always have
while (var >=5) value >=5 ,so the loop would never
{ end.
printf("%d", var);
var++;
}
return 0;
}
Example 2
#include <stdio.h>
int main()
{
int var =5;
Infinite loop: var value will keep
while (var <=10) decreasing because of - - operator,
{ hence it will always be <= 10.
printf("%d", var);
var--;
}
return 0;
}
Example of while loop using logical operator
include <stdio.h>
int main()
{
int i=1, j=1; Output
while (i <= 4 || j <= 3)
{
printf("%d %d\n",i, j); 1 1
i++;
2 2
j++;
} 3 3
return 0;
4 4
}
What are Nested Loops?
• If a loop exists inside another loop, then it is a nested
loop.
OUTER LOOP controls how many iterations
the inner loop will undergo
INNER LOOP Executes multiple times
Statements;
Nested While Loop- Example
#include<stdio.h>
int main()
{
int i = 0, j;
while(i <= 5)
{ Output
printf("%d\t", i);
j = i + 1; 0 1 2 3 4 5
while(j <= 5) 1 2 3 4 5
{ 2 3 4 5
printf("%d\t", j); 3 4 5
j = j + 1; 4 5
} 5
i = i + 1;
printf("\n");
}
return 0;
}
For Loop
• The for loop in C language is used to iterate the statements
or a part of the program several times.
The syntax of the for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
For loop syntax
How for loop works?
•The initialization statement is executed only once.
•Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
•However, if the test expression is evaluated to true, statements inside the body
of for loop are executed, and the update expression is updated.
•Again the test expression is evaluated.
Prints the numbers from 1 to 100 in increments of 1.
for ( int x = 1; x <= 100 ; x++ )
{
printf("%d\n",x);
}
Prints the numbers from 100 to 1 in increments of -1.
for(int x = 100 ; x >= 1; x--)
{
printf("%d\n",x);
}
The following code prints : 2, 7, 12, 17, 22, 27
for(int x = 2; x <= 30 ; x += 5)
{
printf("%d\n",x);
}
Example 1: for loop
/ Print numbers from 1 to 10
#include <stdio.h>
int main()
{
int i; Output
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
1 2 3 4 5 6 7 8 9 10
}
return 0;
}
Example 2: for loop
Program to calculate the sum of first n natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: "); Output
scanf("%d", &num);
// for loop terminates when num is less than count Enter a positive integer: 10
for(count = 1; count <= num; ++count)
{ Sum = 55
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
do-while loop
A do while loop is similar to while loop with one exception that it executes the
statements inside the body of do-while before checking the condition.
In the while loop, first the condition is checked and then the statements in while
loop are executed.
If a condition is false at the first place then the do while would run once,
however the while loop would not run at all.
do - while loop – syntax.
Example of do while loop
•
•
•
The continue statement is used inside loops.
When the continue statement is executed in the loop, the code inside the loop
following the continue statement will be skipped and next iteration of the
loop will begin.
The break statement is used inside loops and switch case.
When a break statement is encountered inside a loop, the control directly comes out of loop
and the loop gets terminated. It is used with if statement whenever used inside loop.
This can also be used in switch case control structure. Whenever it is encountered in switch-
case block, the control comes out of the switch-case
Syntax:
break;
If we don’t use the break statement after every case
block then the output of this program would be: