Looping 1 What do we mean by Loop
Looping 1
What do we mean by Loop Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop n 2
Example Read 5 integers and display their sum 3
Example counter ← 1, sum ← 0 counter < 6 Read 5 integers and display their sum false true input n sum ← sum + n counter = counter + 1 output sum 4
Example Given an exam marks as input, display the appropriate message based on the rules below: q If marks is greater than 49, display “PASS”, otherwise display “FAIL” q However, for input outside the 0 -100 range, display “WRONG INPUT” and prompt the user to input again until a valid input is entered 5
input m m<0 || m>100 false true “WRONG INPUT” input m m>49 true “PASS” false “FAIL” 6
input m m<0 || m>100 false true “WRONG INPUT” input m m>49 true “PASS” false “FAIL” 7
n Implement Loop in C language 8
Looping: while statement while (expression) statement; while (expression) { Block of statements; } The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed. Then the expression is evaluated again and the same thing repeats. The loop terminates when the expression evaluates to 0. 9
Looping: while statement while (expression) statement; while (expression) { Block of statements; } expression False True statement (loop body) 10
Looping: while statement while (expression) statement; while (expression) { Block of statements; } expression False True statement (loop body) The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed. Then the expression is evaluated again and the same thing repeats. The loop terminates when the expression evaluates to 0. 11
Problem: Take a number n as an input. Print all the numbers starting from 1 to n. n N=3 Line 1 Line 2 Line 3 12
Example int i = 1, n; scanf(“%d”, &n); while (i <= n) { printf (“Line no : %dn”, i); i = i + 1; } 13
Are you overweight? n Ask the weight. If weight greater than 65, give suggestions (? ) 14
Example int weight; scanf(“%d”, &weight); while ( weight > 65 ) { printf ("Go, exercise, "); printf ("then come back. n"); printf ("Enter your weight: "); scanf ("%d", &weight); } 15
Sum of first N natural numbers 16
Sum of first N natural numbers void main() { int N, count, sum; scanf (“%d”, &N) ; sum = 0; count = 1; while (count <= N) { sum = sum + count; count = count + 1; } printf (“Sum = %dn”, sum) ; } 17
Looping: for Statement n Most commonly used looping structure in C expr 1 (init) : initialize parameters for ( expr 1; expr 2; expr 3) expr 2 (test): test condition, loop statement; continues if expression is non-0 for ( expr 1; expr 2; expr 3) expr 3 (update): used to alter the { value of the parameters after Block of statements; each iteration } statement (body): body of loop 18
expr 1 (init) expr 2 (test) False True statement (body) expr 3 (update) 19
n Computing Factorial 20
Example: Computing Factorial void main () { int N, count, prod; scanf (“%d”, &N) ; prod = 1; for (count = 1; count <= N; ++count) prod = prod * count; printf (“Factorial = %dn”, prod) ; } 21
Equivalence of for and while for ( expr 1; expr 2; expr 3) statement; Same as expr 1; while (expr 2) { statement expr 3; } 22
Sum of first N Natural Numbers void main () { int N, count, sum; scanf (“%d”, &N) ; sum = 0; count = 1; while (count <= N) { sum = sum + count; void main () { count = count + 1; int N, count, sum; } scanf (“%d”, &N) ; printf (“%dn”, sum) ; sum = 0; } for (count=1; count <= N; ++count) sum = sum + count; printf (“%dn”, sum) ; } 23
The break Statement Break out of the loop body { } ¨ can use with while, do while, for, switch ¨ does not work with if, else n Causes immediate exit from a while, do/while, for or switch structure n Program execution continues with the first statement after the structure n 24
Looping: do-while statement do statement; while (expression); do { Block of statements; } while (expression); statement False expression True 25
Example Problem: Prompt user to input “month” value, keep prompting until a correct value of month is given as input do { printf (“Please input month {1 -12}”); scanf (“%d”, &month); } while ((month < 1) || (month > 12)); 26
x=5 Few tricks #include<stdio. h> n If(); n else; n While(); x=9 int main() { int x; printf("enter xn"); scanf("%d", &x); if(x==9); { printf("goodn"); } else printf("badn"); } Error 27
x=5 Few tricks #include<stdio. h> n If(); n else; n While(); int main() { int x; printf("enter xn"); scanf("%d", &x); x=9 if(x==9) { printf("goodn"); } else; printf("badn"); } 28
Specifying “Infinite Loop” for (; ; ) { statements } while (1) { statements } do { statements } while (1); 29
The continue Statement n n n Skips the remaining statements in the body of a while, for or do/while structure ¨ Proceeds with the next iteration of the loop while and do/while loop ¨ Loop-continuation test is evaluated immediately after the continue statement is executed for loop ¨ expr 3 is evaluated, then expr 2 is evaluated 30
An Example with break and continue void main() { int fact = 1, i = 1; while (1) { fact = fact * i; ++i; if ( i <=10 ) continue; /* not done yet ! Go to loop and perform next iteration*/ break; } } 31
- Slides: 31