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

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

Embed Size (px)

Citation preview

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

Introduction to Computing Introduction to Computing Lecture Lecture 0707::

Repetition and Loop Repetition and Loop Statements (Part IStatements (Part III))

Assist.Prof.Assist.Prof.Dr. Dr. NükhetNükhet ÖZBEKÖZBEKEgeEge University University

Department of Department of Electrical & ElectronicsElectrical & Electronics Engineering Engineeringnukhetnukhet..ozbekozbek@@egeege.edu.tr.edu.tr

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

TopicsTopics

breakbreak statement statement continuecontinue statement statement Infinite loopsInfinite loops Nested loopsNested loops whilewhile and and forfor MacrosMacros ExampleExample

FactorizationFactorization

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

The The breakbreak statement statement

• Implements the "exit loop" primitiveImplements the "exit loop" primitive

• Causes flow of control to leave a loop Causes flow of control to leave a loop block (block (whilewhile or or forfor) immediately) immediately

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

The The continuecontinue statement statement

• Causes flow of control to start Causes flow of control to start immediately the next iteration of a immediately the next iteration of a loop block (loop block (whilewhile or or forfor))

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

Infinite LoopsInfinite 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:Use an:

if ( if ( conditioncondition ) ) {{ break;break; }}

statement to break the statement to break the looploop

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

Example: addpos.c Read in numbers, and Read in numbers, and

addaddonly the positive ones. only the positive ones.

QuitQuitwhen when input input is is 00

set sum to 0set sum to 0

looploop{ { 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 loopexit loop }} add number to sum add number to sum } }

output sumoutput sum

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

include <stdio.h>include <stdio.h>

/****************************/****************************** Read in numbers, and add** Read in numbers, and add** ** onlyonly the positive ones. the positive ones.** Quit when ** Quit when inputinput is is 00*****************************/*****************************/

int main()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 Read in numbers, and addadd

only the positive ones. only the positive ones. QuitQuit

when when input input is is 00

set sum to 0set sum to 0

looploop{ { input numberinput 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 loopexit loop }} add number to sum add number to sum }}

output sumoutput sum

Example: addpos.c (cont)

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

include <stdio.h>include <stdio.h>

/****************************/****************************** Read in numbers, and add** Read in numbers, and add** only the positive ones.** only the positive ones.** Quit when ** Quit when inputinput is is 00*****************************/*****************************/

int main()int main(){{ float num, sum = 0.0;float num, sum = 0.0;

while (while (11)) {{

scanf("%f", &numscanf("%f", &num););

sum += num;sum += num; }}

printf("sum = %f\n", sum);printf("sum = %f\n", sum); return 0;return 0;}}

Read in numbers, and Read in numbers, and addadd

only the positive ones. only the positive ones. QuitQuit

when when input input is is 00

set sum to 0set sum to 0

looploop{ { input numberinput 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 loopexit loop } } add number to sumadd number to sum } }

output sumoutput sum

Example: addpos.c (cont)

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

include <stdio.h>include <stdio.h>

/****************************/****************************** Read in numbers, and add** Read in numbers, and add**** only the positive ones.only the positive ones.** Quit when ** Quit when inputinput is is 00*****************************/*****************************/

int main()int main(){{ float num, sum = 0.0;float num, sum = 0.0;

while (while (11)) {{

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 Read in numbers, and addadd

only the positive ones. only the positive ones. QuitQuit

when when input input is is 00

set sum to 0set sum to 0

looploop{ { 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 loopexit loop }} add number to sum add number to sum } }

output sumoutput sum

Example: addpos.c (cont)

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

include <stdio.h>include <stdio.h>

/****************************/****************************** Read in numbers, and add** Read in numbers, and add**** only the positive ones.only the positive ones.** Quit when ** Quit when input is 0input is 0*****************************/*****************************/

int main()int main(){{ float num, sum = 0.0;float num, sum = 0.0;

while (while (11)))) {{

scanf("%f", &numscanf("%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 Read in numbers, and addadd

only the positive ones. only the positive ones. QuitQuit

when when input input is is 00

set sum to 0set sum to 0

looploop{ { 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 loopexit loop }} add number to sum add number to sum } }

output sumoutput sum

Example: addpos.c (cont)

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

include <stdio.h>include <stdio.h>

/****************************/****************************** Read in numbers, and add** Read in numbers, and add** only the positive ones.** only the positive ones.** Quit when ** Quit when input is 0input is 0*****************************/*****************************/

int main()int main(){{ float num, sum = 0.0;float num, sum = 0.0;

while (while (11)) {{

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 Read in numbers, and addadd

only the positive ones. only the positive ones. QuitQuit

when when input input is is 00

set sum to 0set sum to 0

looploop{ { 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 loopexit loop }} add number to sum add number to sum } }

output sumoutput sum

Example: addpos.c (cont)

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

forfor and and continuecontinue

• In the case of In the case of a a forfor statement statement,, control passes to the “update” control passes to the “update” expression.expression.

for (a = 0; a < 100; a++) { if (a%4) continue;

printf(“%d\n”,a); }

Example:

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

Nested Loops - Example: rect.c

Print an m by n rectangle Print an m by n rectangle ofof

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

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

Example: rect.c (cont) #include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()int main(){{ int int i, i, jj,, m, n; m, n;

printf("\nEnter width: ");printf("\nEnter width: "); scanf("%d", &m);scanf("%d", &m); printf("\nEnter printf("\nEnter heightheight: ");: "); scanf("%d", &n);scanf("%d", &n);

return 0;return 0;}}

Print an m by n Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for for each roweach row{{ for for each columneach column in the current row in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); scanf("%d", &n);scanf("%d", &n);

for (i=0; i <for (i=0; i < nn; i++); i++) {{

}}

return 0;return 0;}}

Print an m by n Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

Example: rect.c (cont)

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); scanf("%d", &n);scanf("%d", &n);

for (i=0; i < n; i++)for (i=0; i < n; i++) {{ for (j=0; j < for (j=0; j < mm; j++); j++) {{ }} }}

return 0;return 0;}}

Print an m by n Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

Example: rect.c (cont)

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); 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 Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

Example: rect.c (cont)

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); 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 Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

Example: rect.c (cont)

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); 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 Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

Example: rect.c (cont)

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

Print an m by n Print an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); 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;}}

programalgorithm

Example: rect.c (cont)

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

#include <stdio.h>#include <stdio.h>

//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/

int main()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 printf("\nEnter heightheight: ");: "); 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;return 0;}}

Variation: rect2.c

PrintPrint an m by n an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{ print an asteriskprint an asterisk }} start next rowstart next row}}

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

#include <stdio.h>#include <stdio.h>//* Print an m-by-n rectangle of * Print an m-by-n rectangle of asterisksasterisks */*/int main()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 printf("\nEnter heightheight: ");: "); 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;}}

PrintPrint an m by n an m by n rectangle ofrectangle of

asterisksasterisks

input width and heightinput width and height

for each rowfor each row{{ for each column in the current row for each column in the current row {{

print an asteriskprint an asterisk

}}

start next rowstart next row}}

Variation: rect3.c

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

#include <stdio.h>#include <stdio.h>/* Print an m-by-n rectangle of /* Print an m-by-n rectangle of

asterisks */asterisks */int main()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 printf("\nEnter heightheight: ");: "); 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)

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

whilewhile and and forfor

• A A forfor loop can always be rewritten loop can always be rewritten as an equivalent as an equivalent whilewhile loop, and loop, and vice-versavice-versa

• TheThe continuecontinue statement in a statement in a forfor loop passes control to the “update” loop passes control to the “update” expression expression

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

ASCII (American Standard Code ASCII (American Standard Code for Information Interchange)for Information Interchange)

• Is a character encoding based on the Is a character encoding based on the English alphabet. English alphabet.

• ASCII codes represent text in ASCII codes represent text in computers, communications computers, communications equipment, and other devices that equipment, and other devices that work with textwork with text

• There are 128 characters (0-127)There are 128 characters (0-127)• First 32 (0-31) are control characters First 32 (0-31) are control characters

and are not printableand are not printable

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

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 <= 127) && (low <if ((low >= 0) && (high <= 127) && (low < high))high)) {{ break;break; }} elseelse {{ printf("Bad bounds. Try again.\n");printf("Bad bounds. Try again.\n"); }} }}

Example: asciiCheck.c

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

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 <= 127) && (low < high))if ((low >= 0) && (high <= 127) && (low < high)) {{ break;break; }} elseelse {{ printf("Bad bounds. Try again.\n");printf("Bad bounds. Try again.\n"); }} }}

Example: asciiCheck.c

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

Example: asciiPrintPrint out a section of the ASCII tablePrint out a section of the ASCII table

for each character from the lower bound to higher boundfor each character from the lower bound to higher bound{{ print its ascii value and ascii characterprint its ascii value and ascii character}}

for ( for ( ch = lowch = low; ; ch <= highch <= high; ; ch++ch++ ) ) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); }} asciiPrint1.c

ch = lowch = low;; while ( while ( ch <= high ch <= high )) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); ch++ch++;; }} asciiPrint2.c

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

for ( for ( ch = lowch = low; ; ch <= highch <= high; ; ch++ch++ ) ) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); }}

ch = lowch = low;; while (1)while (1) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); if (if (ch < highch < high)) {{ ch++;ch++; continuecontinue;; }} elseelse {{ breakbreak;; }} }} asciiPrint3.c

asciiPrint1.c

Example: asciiPrint (cont)

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

for ( for ( ch = lowch = low; ; ch <= highch <= high; ; ch++ch++ ) ) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); }}

ch = lowch = low;; for (;;)for (;;) {{ printf("%d: %c\n", ch, ch);printf("%d: %c\n", ch, ch); ch++ch++;; if (if (ch > highch > high)) {{ breakbreak;; }} }}

asciiPrint4.c

Example: asciiPrint (cont)

asciiPrint1.c

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

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 <= MAX)if ((low >= MIN) && (high <= MAX) && (low < high))&& (low < high)) {{ break;break; }} elseelse {{ 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 <stdio.h>#include <stdio.h>

/* Print a section of /* Print a section of the ASCII table */the ASCII table */

#define MIN 0#define MIN 0#define MAX 127#define MAX 127

int main()int main(){{ int low, high;int low, high; char ch;char ch;

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

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 <= MAX)if ((low >= MIN) && (high <= MAX) && (low < high))&& (low < high)) {{ break;break; }} elseelse {{ 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 <stdio.h>#include <stdio.h>

/* Print a section of /* Print a section of the ASCII table */the ASCII table */

#define MIN 0#define MIN 0#define MAX 127#define MAX 127

int main()int main(){{ int low, high;int low, high; char ch;char ch;

Example: ascii1.c (cont)

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

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 >= if ((low >= MINMIN) && (high <= ) && (high <= MAXMAX)) && (low < high))&& (low < high)) {{ break;break; }} elseelse {{ 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 <stdio.h>#include <stdio.h>

/* Print a section of /* Print a section of the ASCII table */the ASCII table */

#define #define MINMIN 00#define #define MAXMAX 127127

int main()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)

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

Output of Ascii1.cOutput of Ascii1.c

Enter bounds (low high): 32 126Enter 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:342:* 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:G62:> 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:Q72: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:e92:\ 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:o102: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:y112: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:~

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

Example : FactorizationExample : Factorization

• Write a program which prints out the Write a program which prints out the prime factoriprime factorizazation of a number (treat tion of a number (treat 2 as the first prime2 as the first prime))

• For example,For example,

on input 6,on input 6, desired desired output output is is: 2 3: 2 3

" " 24, " " 24, " " " " : : 2 2 2 32 2 2 3

" " 14, " " 14, " " " " : : 2 72 7

" " 23, " " 23, " " " " : : 23 23 ((23 23 is primeis prime))

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

input n

set factor to 2

Algorithm

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

input n

set factor to 2

while(some factor yet to try){

}

Algorithm (cont)

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

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)

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

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)

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

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?

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

#include <stdio.h>

/* Print out the prime factors of a number */

int main(){ int n, factor ;

return 0;}

Program

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

#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)

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

#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 */

printf("\n\n"); return 0;}

Program (cont)

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

/* Try each possible factor in turn */

factor = 2; while ( factor <= n && n > 1 ) {

}

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

/* 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 ; }

}

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

/* 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++ ; } }

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

/* 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++ ; } }

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

#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

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

/* 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++ ; } }

Change from while-loop to for-

loop?

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

/* 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++ ; } }

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

#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