Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while...

Post on 01-Jan-2016

214 views 0 download

Transcript of Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while...

Lecture 8: Choosing the Correct Loop

do … while Repetition StatementSimilar to the while statementCondition for repetition only tested after the body of the loop is performed.

All actions are performed at least once.Format

do { statement(s); } while ( condition );

Flowcharting the do…while repetition statement

Suggestion: always include braces in a do…while statement even if the braces are not nessary.

do … while Repetition Statement

Example 1 (letting counter = 1):do { printf( "%d ", counter );} while (++counter <= 10);

Prints the integers from 1 to 10

Example 2:Simple calculator using do…while statement

Can now do something before testing a condition to see if we should repeat it."code, while, code" can now do "do code, while"

Flowcharting for the simple calculator

Using the while statement

begin

Print the calculator menu;Input the operation;

falseend

true

case Additiontrue Input two numbers;

Perform addition;Print result;

break

false

case Subtractiontrue Input two numbers;Perform subtraction;Print result;

break

false

case multiplicationtrue Input two numbers;Perform multiplication;Print result;

break

false

case divisiontrue Input two numbers;

Perform division;Print result;

break

false

default Print message

operation != Exit

Print the calculator menu;Input the operation;

case Exittrue

Print message break

false

Flowcharting for the simple calculator

Using the do…while statement

begin

Print the calculator menu;Input the operation;

falseend

case Additiontrue Input two numbers;

Perform addition;Print result;

break

false

case Subtractiontrue Input two numbers;Perform subtraction;Print result;

break

false

case multiplicationtrue Input two numbers;Perform multiplication;Print result;

break

false

case divisiontrue Input two numbers;

Perform division;Print result;

break

false

default Print message

operation != Exit

case Exittrue

Print “Thanks” break

false

true

break Statement

Cause immediate exit from a while, for, do…while or switch statement.Program execution continues with the first statement after the structureCommon uses of the break statement

Escape early from a loopSkip the remainder of a switch statement

break StatementExample 1 /* Fig. 4.11 : fig04_11 .c

2 Usin g the break sta tement in a for sta tement */

3 #i ncl ude <stdio .h>

4

5 /* fu nction ma in beg ins program execution */

6 int main ( void )

7 {

8 in t x; /* counter */

9

10 /* loop 10 time s */

11 for ( x = 1; x <= 10; x++ ) {

12

13 /* if x is 5, termi nate loop */

14 if ( x == 5 ) {

15 break ; /* break loop onl y if x is 5 */

16 } /* end if */

17

18 printf( "%d ", x ); /* display value of x */

19 } /* end for */

20

21 prin tf( "\nBroke ou t of loop at x == %d\n", x );

22

23 ret urn 0; /* indica te program ended successf ull y */

24

25 } /* e nd function ma in */

break immediately ends for loop

continue StatementSkips the remaining statements in the body of a while, for or do…while statement.

Proceeds with the next iteration of the loopwhile and do…while

Loop-continuation test is evaluated immediately after the continue statement is executed

The for statementIncrement expression is executed, then the loop-continuation test is evaluated.

continue StatementExample 1 /* Fig. 4.12 : fig04_12 .c

2 Usin g the continu e sta teme nt in a for sta teme nt */

3 #i ncl ude <stdio .h>

4

5 /* fu nction ma in beg ins program execution */

6 int main ( void )

7 {

8 in t x; /* counter */

9

10 /* loop 10 time s */

11 for ( x = 1; x <= 10; x++ ) {

12

13 /* if x is 5, continu e with nex t itera tio n of loop */

14 if ( x == 5 ) {

15 continu e; /* skip remai ning code in loop body */

16 } /* end if */

17

18 printf( "%d ", x ); /* display value of x */

19 } /* end for */

20

21 prin tf( "\nUsed conti nue to skip print ing the value 5\n" );

22

23 ret urn 0; /* indica te program ended successf ull y */

24

25 } /* e nd function ma in */

continue skips to end of for loop and performs next iteration

Write a program that checks to see if a number the user inputs is a prime number or not.  If the number is prime, the program should exit.  Otherwise, the program should keep asking the user for a new number.

Use a do/while loop to control the looping. Provide appropriate feedback to the user (prime or not). Use the % operator to conduct your checks.  (Hint: The %

operator returns the remainder.) Try to make the program as short as possible. 

Submit your completed program in the Prime Numbers dropbox. 

In-Class Programming Exercise

Challenge: 4.17

In mathematics, a prime number is a positive integer which has exactly two distinct divisors: 1 and itself.

• If none of 2, 3, …, n/2 is a divisor of n, than n is prime.• If one of 2, 3, …, n/2 is a divisor of n, than n is NOT

prime.

Prime Number

begin

Input the number (> 2)

tag==TRUE false

true

Determine if the number is primeand set the tag

end

Print the number is prime.

begin

Input the number (> 2)

end

tag==TRUE

true

false

divisor = 2tag = TRUE

remainder==0

divisor ++;

divisor<=number/2

remainder = number % divisor

tag = FALSE;break;

true

false

falsePrint the number is prime.true

Determine if the number is prime and set the tag