Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions."— Presentation transcript:

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

2 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 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 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 5 Example – Fibonacci Series fibonacci.c

6 6 Fibonacci – step by step 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 Screen 5 lim 0 fib1 1 fib2 --- fib_next

7 7 Fibonacci – step by step 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 Screen 5 lim 0 fib1 1 fib2 --- fib_next

8 8 Fibonacci – step by step 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 Screen 5 lim 0 fib1 1 fib2 --- fib_next

9 9 Fibonacci – step by step 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 Screen 5 lim 0 fib1 1 fib2 1 fib_next

10 10 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 1 fib_next

11 11 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 1 fib_next

12 12 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 1 fib_next

13 13 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 1 fib_next

14 14 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 2 fib_next

15 15 Fibonacci – step by step 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 Screen 5 lim 1 fib1 1 fib2 2 fib_next

16 16 Fibonacci – step by step 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 Screen 5 lim 1 fib1 2 fib2 2 fib_next

17 17 Fibonacci – step by step 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 Screen 5 lim 1 fib1 2 fib2 2 fib_next

18 18 Fibonacci – step by step 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 2 Screen 5 lim 1 fib1 2 fib2 2 fib_next

19 19 Fibonacci – step by step 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 2 Screen 5 lim 1 fib1 2 fib2 3 fib_next

20 20 Fibonacci – step by step 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 2 Screen 5 lim 2 fib1 2 fib2 3 fib_next

21 21 Fibonacci – step by step 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 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next

22 22 Fibonacci – step by step 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 2 Screen 5 lim 2 fib1 3 fib2 3 fib_next

23 23 Fibonacci – step by step 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 2 3 Screen 5 lim 2 fib1 3 fib2 3 fib_next

24 24 Fibonacci – step by step 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 2 3 Screen 5 lim 2 fib1 3 fib2 5 fib_next

25 25 Fibonacci – step by step 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 2 3 Screen 5 lim 3 fib1 3 fib2 5 fib_next

26 26 Fibonacci – step by step 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 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next

27 27 Fibonacci – step by step 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 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next

28 28 Fibonacci – step by step 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 2 3 Screen 5 lim 3 fib1 5 fib2 5 fib_next

29 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 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 31 Example – Power of Two Input: integer A Output: is there an integer N such that A == 2^N? Solution: powerOfTwo.c

32 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 33 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions

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

35 35 Order of Execution 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); 2 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 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 37 Factorial using for – step by step #include 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; } --- i 3 n 1 fact

38 38 Factorial using for – step by step #include 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 i 3 n 1 fact

39 39 Factorial with for – step by step #include 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 i 3 n 1 fact

40 40 Factorial with for – step by step #include 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 i 3 n 1 fact

41 41 Factorial with for – step by step #include 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 i 3 n 2 fact

42 42 Factorial with for – step by step #include 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 i 3 n 2 fact

43 43 Factorial with for – step by step #include 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 i 3 n 6 fact

44 44 Factorial with for – step by step #include 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 i 3 n 6 fact

45 45 Factorial with for – step by step #include 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 i 3 n 6 fact

46 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 47 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions

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

49 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 50 Infinite Loops What are they? Beware of them

51 51 ‘break’ in Loops When break is encountered, the loop exits regardless of whether the condition’s state The program then continues to run from the first line after the loop If called within a nested loop, break breaks out of the inner loop only

52 52 ‘continue’ in Loops When continue is encountered, the rest of the current’s loop’s iteration is ignored The program then continues to run from the beginning of the loop

53 53 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions

54 54 Example: Rectangle of ‘*’ /* Print a rectangle of *. The height and width are defined by the user */ int i,j; int height, width; printf("Please enter the two box dimensions: \n"); scanf("%d%d",&height,&width); for (i = 1; i <= height; i++) { for(j = 1; j <= width; j++) { printf("*"); } printf("\n"); }

55 55 Example Write a program that accepts a number from the user, and prints out all of the prime numbers up to that number (Hints: nested loops, part of the solution was seen during lecture)

56 56 Solution (listprimes.c) int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i = 2; i <= last; i = i + 1) { for(j = 2 ; j < i; j = j + 1) { if (i % j == 0) { // i is not a prime break; } if (j == i) { // j "passed" all test and thus is a prime printf("the number %d is prime\n", i); }

57 57 Exercise Change the former prime-listing program, so that is displays only the largest prime number which is smaller than or equal to the user’s input

58 58 Solution 1 (largest_prime.c) int i, j, last; int found = 0; /* This indicates whether we found the largest prime */ printf("enter a number\n"); scanf("%d", &last); i = last; while (!found) {/* Loop until we find the required prime */ for (j = 2 ; j < i; j = j + 1) if (i % j == 0) break; if (j == i) /* If this is true then i is prime */ found = 1; else i--; } printf("The largest prime not larger than %d is %d.\n", last, i);

59 59 Solution 2 ( largest_prime_2for.c ) int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i = last; i > 1; i = i - 1) { for(j = 2 ; j < i; j = j + 1) if (i % j == 0) break; // inner loop if (j == i) /* i is prime */ break; } printf("The largest prime not larger than %d is %d.\n", last, i);

60 60 Exercise Write a program that prints an upside- down half triangle of *. The height of the pyramid is the input. ***** *** ** **** *

61 61 Solution (triangle.c) #include int main(void) { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for(j = i; j <= size; j++) printf("*"); printf("\n"); } return 0; }

62 62 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions

63 63 do-while Loops do { body } while (condition); Similar to while loops Except the condition is evaluated after the loop body The loop body is always executed at least once, even if the expression is never true

64 64 Example: wait for legal input int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) { printf("That's not a positive number! Try again.\n"); } } while (i<=0); /* The program continues.... */

65 65 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions

66 66 getchar getchar() gets a single character from the user. Requires including stdio.h Returns a non-positive number on failure. Similar to scanf. char c; c = getchar(); char c; scanf(“%c”, &c);  ==== 

67 67 putchar putchar(‘char’) prints out the character inside the brackets. Requires including stdio.h Similar to printf. char c; putchar(c); char c; printf(“%c”, c);  ==== 

68 68 More Operators Used as a short-hand for incrementing (or decrementing) variables. i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *= a  ==  i = i * a i /= a  ==  i = i / a

69 69 Q1 עיינו בקטע הבא וסמנו את כל התשובות הנכונות : int k = 10, sum = 0; int j; for (j = 0;j < k; j = k - 1) sum = sum + 1; a. After execution the value of k is 10 b. This code will fail in compilation c. After execution the value of j is 9 d. There will be an infinite loop e. Non of these answers is correct

70 70 Q2 עיינו בקטע הבא וסמנו את כל התשובות הנכונות : int sum = 0,number; for (number = 1;number <= 10; number = number + 1) number = number - 1; printf(“%d”,sum); a. This code will fail in compilation b. 0 will be printed c. 55 will be printed d. 45 will be printed e. Non of these answers is correct

71 71 Q3 עיינו בקטע הבא וסמנו את כל התשובות הנכונות : int i; for (i = 0; i < 10; i = i + 1) for (i = 0; i < 9; i = i + 1) System.out.println(“*”); a. 90 ‘*’ will be printed b. There will be an infinite loop c. 10 ‘*’ will be printed d. This code will fail in compilation e. Non of these answers is correct

72 72 Using the Debugger


Download ppt "1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions."

Similar presentations


Ads by Google