Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.

Similar presentations


Presentation on theme: "Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part."— Presentation transcript:

1 Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

2 Topics  break statement  continue statement  Infinite loops  Nested loops  while and for  Macros  Example  Factorization

3 The break statement Implements the "exit loop" primitive Implements the "exit loop" primitive Causes flow of control to leave a loop block ( while or for ) immediately Causes flow of control to leave a loop block ( while or for ) immediately

4 The continue statement Causes flow of control to start immediately the next iteration of a loop block ( while or for ) Causes flow of control to start immediately the next iteration of a loop block ( while or for )

5 Infinite Loops while ( 1 ) while ( 1 ) {...etc...etc...etc......etc...etc...etc... } for ( ; 1 ; ) for ( ; 1 ; ) {...etc...etc...etc......etc...etc...etc... } for ( ; ; ) for ( ; ; ) {...etc...etc...etc......etc...etc...etc... } Use an: if ( condition ) if ( condition ) { break; break; } statement to break the loop

6 Example: addpos.c Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum

7 include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

8 include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

9 include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

10 include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1)) while (1)) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

11 include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

12 for and continue In the case of a for statement, control passes to the “update” expression. In the case of a for statement, control passes to the “update” expression. for (a = 0; a < 100; a++) { if (a%4) continue; printf(“%d\n”,a); } Example:

13 Nested Loops - Example: rect.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

14 Example: rect.c (cont) #include #include /* Print an m-by-n rectangle of /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

15 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

16 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { } } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

17 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

18 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

19 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

20 Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} program algorithm Example: rect.c (cont)

21 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); i = 0; i = 0; while (i < n) while (i < n) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); i++; i++; } return 0; } Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

22 #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { j = 0; j = 0; while (1) while (1) { printf("*"); printf("*"); j++; j++; if (j == m) if (j == m) break; break; } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Variation: rect3.c

23 #include #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { j = 0; j = 0; while (1) while (1) { printf("*"); printf("*"); j++; j++; if (j == m) if (j == m) break; break; } printf("\n"); printf("\n"); } return 0; return 0;} The innermost enclosing loop for this break is the while-loop Variation: rect3.c (cont)

24 while and for A for loop can always be rewritten as an equivalent while loop, and vice-versa A for loop can always be rewritten as an equivalent while loop, and vice-versa The continue statement in a for loop passes control to the “update” expression The continue statement in a for loop passes control to the “update” expression

25 ASCII (American Standard Code for Information Interchange) Is a character encoding based on the English alphabet. Is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text ASCII codes represent text in computers, communications equipment, and other devices that work with text There are 128 characters (0-127) There are 128 characters (0-127) First 32 (0-31) are control characters and are not printable First 32 (0-31) are control characters and are not printable

26 while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= 0) && (high = 0) && (high <= 127) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } Example: asciiCheck.c

27 while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= 0) && (high = 0) && (high <= 127) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } Example: asciiCheck.c

28 Example: asciiPrint Print out a section of the ASCII table for each character from the lower bound to higher bound { print its ascii value and ascii character print its ascii value and ascii character} for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } asciiPrint1.c ch = low; ch = low; while ( ch <= high ) while ( ch <= high ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); ch++; ch++; } asciiPrint2.c

29 for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } ch = low; ch = low; while (1) while (1) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); if (ch < high) if (ch < high) { ch++; ch++; continue; continue; } else else { break; break; } } asciiPrint3.c asciiPrint1.c Example: asciiPrint (cont)

30 for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } ch = low; ch = low; for (;;) for (;;) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); ch++; ch++; if (ch > high) if (ch > high) { break; break; } } asciiPrint4.c Example: asciiPrint (cont) asciiPrint1.c

31 while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} Example: ascii1.c #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch;

32 while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch; Example: ascii1.c (cont)

33 while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch; Macro definition: #define identifier tokens All subsequent instances of identifier are replaced with its token Example: ascii1.c (cont)

34 Output of Ascii1.c Enter bounds (low high): 32 126 32: 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 32: 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 62:> 63:? 64:@ 65:A 66:B 67:C 68:D 69:E 70:F 71:G 62:> 63:? 64:@ 65:A 66:B 67:C 68:D 69:E 70:F 71:G 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 122:z 123:{ 124:| 125:} 126:~ 122:z 123:{ 124:| 125:} 126:~

35 Example : Factorization Write a program which prints out the prime factorization of a number (treat 2 as the first prime) Write a program which prints out the prime factorization of a number (treat 2 as the first prime) For example, For example, on input 6, desired output is: 2 3 on input 6, desired output is: 2 3 " " 24, " " : 2 2 2 3 " " 24, " " : 2 2 2 3 " " 14, " " : 2 7 " " 14, " " : 2 7 " " 23, " " : 23 (23 is prime) " " 23, " " : 23 (23 is prime)

36 input n set factor to 2 Algorithm

37 input n set factor to 2 while(some factor yet to try) { } Algorithm (cont)

38 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } } Algorithm (cont)

39 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } Algorithm (cont)

40 input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } increment factor } Algorithm (cont) Why not?

41 #include /* Print out the prime factors of a number */ int main() { int n, factor ; return 0; } Program

42 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; return 0; } Program (cont)

43 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ printf("\n\n"); return 0; } Program (cont)

44 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { }

45 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; }

46 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

47 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

48 #include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } printf("\n\n"); return 0; } factor1.c

49 /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } Change from while -loop to for - loop?

50 /* Try each possible factor in turn */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

51 #include /* Print out the prime factors of a number. */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn. */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor. */ factor++ ; } printf("\n\n"); return 0; } factor2.c


Download ppt "Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part."

Similar presentations


Ads by Google