Lecture 8: Choosing the Correct Loop
do … while Repetition Statement Similar to the while statement Condition 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; false end true case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit Print the calculator menu; Input the operation; case Exit true Print messagebreak false
Flowcharting for the simple calculator Using the do…while statement begin Print the calculator menu; Input the operation; false end case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit case Exit true 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 structure Common uses of the break statement Escape early from a loop Skip the remainder of a switch statement
break Statement Example break immediately ends for loop
continue Statement Skips the remaining statements in the body of a while, for or do…while statement. Proceeds with the next iteration of the loop while and do…while Loop-continuation test is evaluated immediately after the continue statement is executed The for statement Increment expression is executed, then the loop- continuation test is evaluated.
continue Statement Example 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 prime and set the tag end Print the number is prime.
begin Input the number (> 2) end tag==TRUE true false divisor = 2 tag = TRUE remainder==0 divisor ++; divisor<=number/2 remainder = number % divisor tag = FALSE; break; true false Print the number is prime. true Determine if the number is prime and set the tag