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
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
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; 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 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; 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; 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 add number to sum output 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); if (num < 0) continue; else if (num == 0) break; sum += num; 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; add number to sum sum += num; 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) Print an m by n rectangle of asterisks input width and height #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 each row for each column in the current row print an asterisk start next row return 0;
Example: rect.c (cont) 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 #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++) return 0;
Example: rect.c (cont) 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 #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++) for (j=0; j < m; j++) return 0;
Example: rect.c (cont) 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 #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++) for (j=0; j < m; j++) printf("*"); return 0;
Example: rect.c (cont) 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 #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++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;
Example: rect.c (cont) 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 #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++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;
Example: rect.c (cont) Print an m by n rectangle of asterisks algorithm input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; program 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("*"); printf("\n"); 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 print an asterisk start next row #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); 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 input width and height #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 each row for each column in the current row print an asterisk start next row for (i=0; i < n; i++) j = 0; while (1) printf("*"); j++; if (j == m) break; printf("\n"); return 0;
Variation: rect3.c (cont) #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; The innermost enclosing loop for this break is the while-loop 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;
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 #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; 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"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;
Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; 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"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;
Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; 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"); Macro definition: for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); #define identifier tokens return 0; All subsequent instances of identifier are replaced with its token
Output of Ascii1.c Enter bounds (low high): 32 126 32: 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 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:= 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 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 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 122:z 123: 124: 125: 126:~
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, " " : 2 2 2 3 " " 14, " " : 2 7 " " 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) 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
#include <stdio.h> Program /* Print out the prime factors of a number */ int main() int n, factor ; return 0;
#include <stdio.h> Program (cont) /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; return 0;
#include <stdio.h> Program (cont) /* 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;
/* 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 are: ", n) ; /* 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++ ; 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 multiple while-loop of factor, to for- ** so print factor and divide n by factor */ loop? 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