Lec-9+10 Loop
Lec-9+10 Loop
#include <stdio.h>
int main() {
printf( "Hello World\n");
}
Why Loop is used?
▪ Write a code to print hello world 10 times
#include <stdio.h>
int main() {
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n"); }
Why Loop is used?
▪ Loops in C programming is used Solution 1:
#include <stdio.h>
when we need to execute a block
int main() {
of statements repeatedly. printf( "Hello World\n");
▪ For example: Suppose we want to printf( "Hello World\n");
print “Hello World” 10 times. printf( "Hello World\n");
How can this be done efficiently? printf( "Hello World\n");
printf( "Hello World\n");
▪ Using loop statement, we could printf( "Hello World\n");
easily solve this problem without printf( "Hello World\n");
writing the printf statement 10 printf( "Hello World\n");
times. printf( "Hello World\n");
printf( "Hello World\n");
}
Why Loop is used?
▪ In looping, a sequence of statements are executed until some
conditions for termination of the loop are satisfied.
▪ In short, in loop statement, we can specify how many times a
block of statement will be executed.
▪ There are three loop statements in C:
▪ for loop
▪ while loop
▪ do…while loop
for Loop - syntax
▪ For loop is used to repeat a statement or a block of statements a
specific number of times.
▪ Syntax:
for (initialization of loop variable; conditional test; update loop variable) {
// body of the loop
// statements we want to execute
}
for Loop – Execution Steps
for (initialization of loop variable; conditional test; update loop variable) {
// body of the loop
// statements we want to execute
}
1. At first the loop variable is initialized. This statement is executed only
once.
2. Then condition is evaluated-
i. If the condition is false, it returns 0 and the loop is terminated.
ii. if the condition is true, it returns 1, body of the loop is executed, loop variable
is updated
3. Step 2 is repeated until the loop is terminated
for Loop – Flow Chart
1 2 5 4
1 for (initialization of loop variable; conditional test; update loop variable) {
3. If true 6. If false
2 // body of the loop
// statements we want to execute
3 }
5
6
Statement after for loop ;
4
for Loop – Flow Chart
6. If false
1 2 5 4
1 for (initialization of loop variable; conditional test; update loop variable) {
3. If true
3 7 }
5
OUTPUT
Hello World
Hello World
Outside loop body
while Loop Example 1
▪Print all the integer numbers int i=1;
from 1 to 10 inclusive while(i<=10){
printf(“%d\n”,i);
for(i=1; i<=10;i++){ i++;
}
printf(“%d\n”,i);
}
while Loop Example 2
▪Print all the odd numbers from int i=1;
1 to 10 inclusive while(i<=10){
if(i%2!=0){
for(i=1; i<=10;i++){ printf(“%d\n”,i);
}
if(i%2!=0){ i++;
printf(“%d\n”,i); }
}
}
Task – Solve Using while loop
Print the following series up to n and its sum :
1 + 2 + 3 + ... + n
0 + 2 + 4 + ... + n
1+ 3 + 5 + ... + n
do{ 1
1
statement 1;
…………………
2 3. If true
5.
3
statement n;
} while(conditional test) ; 24
do while Loop – Execution Simulation
do{ 1
1
statement 1;
…………………
2 3. If true
3
statement n;
5 } while(conditional test) ; 24
5. If false
Statement outside loop;
Sample do while Loop
1. int i = 0;
2. do{ Execution Value of Condition State
3. printf(“Hello World”); i
4. i++; Loop body i=1 i<2 or 0<2 true
5. } while ( i<2) ; Loop body i=2 i<2 or 1<2 false
6. printf(“Outside loop body”); Outside loop
OUTPUT
Hello World
Hello World
Outside loop body
Difference Between while and do while Loop
▪ In while loop, find the condition is checked, then the body is
executed if condition is true.
▪ In do...while loop, first the body is executed, then condition is
checked.
▪ the body of do...while loop will execute at least once irrespective
to the test-expression.
Difference Between while and do while Loop
int i=0; int i=0;
while(i<0){ do{
printf("I is a positive number"); printf("I is a negative number");
i++; i++;
} }while(i<0);
Difference Between while and do while Loop
int i=0;
while(i<0){
printf("I is a positive number");
i++; Value of i Condition State Execution