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 Topics break statement continue statement Infinite loops Nested loops while and for Macros Example Factorization The break statement • Implements the "exit loop" primitive • Causes flow of control to leave a loop block (while or for) immediately The continue statement • Causes flow of control to start immediately the next iteration of a loop block (while or for) Infinite Loops while ( 1 ) { ...etc...etc...etc... } for ( ; 1 ; ) { ...etc...etc...etc... } for ( ; ; ) { ...etc...etc...etc... } Use an: if ( condition ) { break; } statement to break the loop Example: addpos.c Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } output sum Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; printf("sum = %f\n", sum); return 0; output sum } Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } add number to sum } include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (1) { scanf("%f", &num); sum += num; } printf("sum = %f\n", sum); return 0; output sum } Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (1) { scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; sum += num; add number to sum } } output sum printf("sum = %f\n", sum); return 0; } Example: addpos.c (cont) include <stdio.h> Read in numbers, and add only the positive ones. Quit when input is 0 /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ set sum to 0 int main() { float num, sum = 0.0; loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } while (1)) { scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; add number to sum sum += num; } } output sum printf("sum = %f\n", sum); return 0; } Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop { input number if (number is negative) { begin next iteration } else if ( number is zero) { exit loop } include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; while (1) { scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; sum += num; add number to sum } } output sum printf("sum = %f\n", sum); return 0; } for and continue • In the case of a for statement, control passes to the “update” expression. Example: for (a = 0; a < 100; a++) { if (a%4) continue; printf(“%d\n”,a); } 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 { print an asterisk } start next row } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); input width and height for each row { for each column in the current row { print an asterisk } start next row } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); input width and height for (i=0; i < n; i++) { for each row { for each column in the current row { print an asterisk } start next row } } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); input width and height for (i=0; i < n; i++) { for (j=0; j < m; j++) { for each row { for each column in the current row { print an asterisk } start next row } } } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; input width and height printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } for each row { for each column in the current row { print an asterisk } start next row } } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; input width and height printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for each row { for each column in the current row { print an asterisk } start next row } for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); input width and height for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } for each row { for each column in the current row { print an asterisk } start next row } return 0; } Example: rect.c (cont) #include <stdio.h> /* Print an m by n rectangle of asterisks Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; algorithm printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); program input width and height for (i=0; i < n; i++) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); } for each row { for each column in the current row { print an asterisk } start next row } return 0; } Variation: rect2.c #include <stdio.h> /* Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row { print an asterisk } start next row } Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); i = 0; while (i < n) { for (j=0; j < m; j++) { printf("*"); } printf("\n"); i++; } return 0; } Variation: rect3.c Print an m by n rectangle of asterisks #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); input width and height for (i=0; i < n; i++) { j = 0; while (1) { printf("*"); j++; if (j == m) break; } printf("\n"); } return 0; for each row { for each column in the current row { print an asterisk } start next row } } Variation: rect3.c (cont) #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; printf("\nEnter width: "); scanf("%d", &m); printf("\nEnter height: "); scanf("%d", &n); for (i=0; i < n; i++) { j = 0; while (1) { printf("*"); j++; if (j == m) break; } printf("\n"); } return 0; The innermost enclosing loop for this break is the while-loop } while and for • 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 ASCII (American Standard Code for Information Interchange) • Is a character encoding based on the English alphabet. • ASCII codes represent text in computers, communications equipment, and other devices that work with text • There are 128 characters (0-127) • First 32 (0-31) are control characters and are not printable Example: asciiCheck.c while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } } Example: asciiCheck.c while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } } 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 } for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); } asciiPrint1.c ch = low; while ( ch <= high ) { printf("%d: %c\n", ch, ch); ch++; } asciiPrint2.c Example: asciiPrint (cont) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); asciiPrint1.c } ch = low; while (1) { printf("%d: %c\n", ch, ch); if (ch < high) { ch++; continue; } else { break; } asciiPrint3.c } Example: asciiPrint (cont) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); asciiPrint1.c } ch = low; for (;;) { printf("%d: %c\n", ch, ch); ch++; if (ch > high) { break; } } asciiPrint4.c Example: ascii1.c while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } #include <stdio.h> /* Print a section of the ASCII table */ #define #define MIN MAX } 0 127 int main() { int low, high; char ch; for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } return 0; } Example: ascii1.c (cont) while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } #include <stdio.h> /* Print a section of the ASCII table */ #define #define MIN MAX } 0 127 int main() { int low, high; char ch; for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } return 0; } Example: ascii1.c (cont) while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } #include <stdio.h> /* Print a section of the ASCII table */ #define #define MIN MAX 0 127 int main() { int low, high; char ch; } for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); } Macro definition: #define identifier tokens return 0; } subsequent instances of identifier All are replaced with its token Output of Ascii1.c Enter bounds (low high): 32 126 32: 33:! 34:" 35:# 42:* 43:+ 44:, 45:52:4 53:5 54:6 55:7 62:> 63:? 64:@ 65:A 72:H 73:I 74:J 75:K 82:R 83:S 84:T 85:U 92:\ 93:] 94:^ 95:_ 102:f 103:g 104:h 105:i 112:p 113:q 114:r 115:s 122:z 123:{ 124:| 125:} 36:$ 46:. 56:8 66:B 76:L 86:V 96:` 106:j 116:t 126:~ 37:% 47:/ 57:9 67:C 77:M 87:W 97:a 107:k 117:u 38:& 48:0 58:: 68:D 78:N 88:X 98:b 108:l 118:v 39:' 49:1 59:; 69:E 79:O 89:Y 99:c 109:m 119:w 40:( 50:2 60:< 70:F 80:P 90:Z 100:d 110:n 120:x 41:) 51:3 61:= 71:G 81:Q 91:[ 101:e 111:o 121:y Example : Factorization • Write a program which prints out the prime factorization of a number (treat 2 as the first prime) • For example, on input 6, desired output is: 2 3 " " 24, " " : 2223 " " 14, " " : 27 " " 23, " " : 23 (23 is prime) Algorithm input n set factor to 2 Algorithm (cont) input n set factor to 2 while(some factor yet to try) { } Algorithm (cont) 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) 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) input n Why not? set factor to 2 while(some factor yet to try) while(some factor yet to try) { { if (n is divisible by factor) if (n is divisible by factor) { { output factor output factor set n to n / factor set n to n / factor } } else increment factor { } increment factor } } Program #include <stdio.h> /* Print out the prime factors of a number */ int main() { int n, factor ; return 0; } Program (cont) #include <stdio.h> /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; return 0; } Program (cont) #include <stdio.h> /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of /* Try each possible factor in turn printf("\n\n"); return 0; } %d are: ", n) ; */ /* Try each possible factor in turn factor = 2; while ( factor <= n && n > 1 ) { } */ /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 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 ; } } /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 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++ ; } } /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 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++ ; } } #include <stdio.h> /* 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 Try each possible factor in turn are: ", n) ; */ factor = 2; while ( factor <= n && n > 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 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) { if (n % factor == 0) Change from { /* n is a multiplewhile-loop of factor,to for** so print factor and divide loop? n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* ** n is not a multiple of factor; try next possible factor */ factor++ ; } } /* Try each possible factor in turn */ for ( factor = 2; factor <= n && n > 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++ ; } } #include <stdio.h> /* 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 <= n && n > 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