Lab 5 4 Using Break and Continue Statements
Lab 5 4 Using Break and Continue Statements
The break and continue statements alter the flow of control in a program. When you
use a break in a switch statement or in a repetition structure, you provide an immediate
exit from the structure. The program continues to execute with the first statement after the
structure.
Use a continue statement in a repetition structure to end the current iteration only and
proceed with the next iteration of the loop. In a for loop, the next statement is the
update statement. Use a break statement in a repetition structure to end the entire
repetition. Use these constructs sparingly. They are introduced for informational purposes
but are not suggested for general solutions.
Objectives
In this lab, you use break and continue statements to alter the control of a loop.
In the following exercises, you evaluate the output in repetition control structures that use
continue or break statements. You also write programs from designs that use
continue or break statements.
1a. Design a program that prompts the user to enter 20 numbers. Add all positive numbers.
Use a for loop with a continue statement to skip negative numbers. Output the total
found.
Write your design in the following space. Your design should be a list of C++ comments
without any code.
1b. Write a C++ program based on the design you created in Exercise 1a and name it
continue.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the output
message.
1c. Enter, compile, link, and execute continue.cpp. Then copy the output and save it in a
block comment at the end of your program. Save continue.cpp in the Chap05 folder of
your Student Data Files.
The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
Write your design in the following space. Your design should be a list of C++ comments
without any code.
2b. Write a C++ program based on the design you created in Exercise 2a and name it
break.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the output
message.
2c. Enter, compile, link, and execute break.cpp. Then copy the output and save it in a block
comment at the end of your program. Save break.cpp in the Chap05 folder of your
Student Data Files.
The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.