720 likes | 938 Views
Agenda - Loops. while for for & while Nested Loops do-while Misc. & Questions. Loops. Used to repeat the same instructions until a stop criterion is met C provides some flexible ways of deciding how many times to loop, or when to exit a loop for, while, do-while loops. while Loops.
E N D
Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions
Loops • Used to repeat the same instructions until a stop criterion is met • C provides some flexible ways of deciding how many times to loop, or when to exit a loop • for, while, do-while loops
while Loops while (condition) { statements; } The statements are executed as long as condition is true When the condition is no longer true, the loop “exits”
Example - Factorial int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i<=n) { fact = fact*i; i = i + 1; } printf("the factorial is %d\n", fact);
Example – Fibonacci Series fibonacci.c
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 1 5 Screen 0 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 2 5 Screen 0 1 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 2 5 Screen 0 1 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1 2
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 3 5 Screen 0 1 1 2
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 2 3 5 Screen 0 1 1 2
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2 3
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 5 5 Screen 0 1 1 23
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 3 5 5 Screen 0 1 1 23
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 5 5 5 Screen 0 1 1 23
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 5 5 5 Screen 0 1 1 23
Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 5 5 5 Screen 0 1 1 23
Example – Integer Division • Input: • Two integers – A and B • Output: • How many times A contains B (it is the result of the integer division A/B) • Do not use the operators ‘/’, ‘*’ • Solution: division.c
Solution int a, b, res, tmp; printf("Please enter two numbers.\n"); scanf("%d %d", &a, &b); tmp = a; res = 0; while(tmp >= b) { tmp = tmp - b; res = res + 1; } printf("%d / %d = %d\n", a, b, res);
Example – Power of Two • Input: integer A • Output: is there an integer N such that A == 2^N? • Solution: powerOfTwo.c
Solution int a, tmp; printf("Please enter a num\n"); scanf("%d", &a); tmp = a; while((tmp > 0) && (tmp % 2 == 0)) { tmp = tmp / 2; } if (tmp == 1) printf("%d is a power of two\n",a); else printf("%d is NOT a power of two\n",a);
Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions
for Loops for (initiate; termination-condition; update) { body } • Initiate • If termination-condition holds: • Execute body • Update • Go to step 2
Order of Execution for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i); 1 for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i); 2 for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i); for( i = 1; i<=10 ; i++) printf(“%d\n”, i);3 for( i = 1; i<=10 ; i++) printf(“%d\n”, i); ……
Factorial (again) int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i = 1; i <= n; i = i + 1) fact = fact * i; printf("the factorial is %d\n", fact);
Factorial using for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } --- 3 1
Factorial using for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 1 3 1
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 1 3 1
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 2 3 1
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 2 3 2
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 3 3 2
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 3 3 6
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 4 3 6
Factorial with for – step by step i n fact #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); for(i=1;i<=n;i++) fact *= i; printf("the factorial is %d\n", fact); return 0; } 4 3 6
Example: Fahrenheit to Celsius Conversion Table /* Print a Fahrenheit-to-Celsius conversion table */ int fahr; double celsius; int lower = 0, upper = 300; int step = 20; for(fahr=lower ; fahr<=upper ; fahr = fahr + step) { celsius = 5.0*(fahr -32.0)/9.0; printf("%d\t%g\n", fahr, celsius); }
Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions
for while for (initiate; termination-condition; update) { body; } initiate; while (termination-condition) { body update; }
When use for/while? • Any for loop can be converted to while loop and vice versa • Some applications are more natural to for, and others to while • for is more suited when something is performed a predefined number of times • while is more suited if the number of iterations is not known in advance (e.g., asking for legal input from a user)
Infinite Loops • What are they? • Beware of them