1 / 72

Agenda - Loops

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.

haines
Download Presentation

Agenda - Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions

  2. 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

  3. 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”

  4. 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);

  5. Example – Fibonacci Series fibonacci.c

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  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"); 2 3 3 5 Screen 0 1 1 2 3

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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);

  31. Example – Power of Two • Input: integer A • Output: is there an integer N such that A == 2^N? • Solution: powerOfTwo.c

  32. 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);

  33. Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions

  34. for Loops for (initiate; termination-condition; update) { body } • Initiate • If termination-condition holds: • Execute body • Update • Go to step 2

  35. 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); ……

  36. 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);

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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); }

  47. Agenda - Loops • while • for • for & while • Nested Loops • do-while • Misc. & Questions

  48. for while for (initiate; termination-condition; update) { body; } initiate; while (termination-condition) { body update; }

  49. 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)

  50. Infinite Loops • What are they? • Beware of them

More Related