0% found this document useful (0 votes)
16 views5 pages

Lab 5 4 Using Break and Continue Statements

Lab 5.4 focuses on using break and continue statements to control loop execution in C++. The objectives include executing loops until a break is encountered and skipping iterations with continue. The lab involves designing and coding programs that utilize these statements to process user input and calculate sums based on specified conditions.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Lab 5 4 Using Break and Continue Statements

Lab 5.4 focuses on using break and continue statements to control loop execution in C++. The objectives include executing loops until a break is encountered and skipping iterations with continue. The lab involves designing and coding programs that utilize these statements to process user input and calculate sums based on specified conditions.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

After completing this lab, you will be able to:


• Execute a loop until a break statement is encountered.
• Execute a loop, skip over the remaining loop statements when a continue
statement is encountered, and proceed in the loop until the loop terminates.

Estimated completion time: 20–30 minutes


Lab 5.4 Steps: Using break and continue Statements

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.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

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.

Enter a list of 20 numbers:


2 3 1 -6 10 2 6 5 9 -10 9 2 -4 3 1 2 -6 7 3 2
The sum of the positive numbers is: 67
2a. Modify the design and rewrite the continue.cpp program you created in Exercise 1. The
program should prompt the user to enter 20 numbers. Add all numbers until the user
enters a negative number. Use a for loop with a break statement to end the loop
when a negative number is entered. Output the total found.

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.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when
Initialized

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.

Enter a list of 20 numbers:


2 3 1 -6 10 2 6 5 9 -10 9 2 -4 3 1 2 -6 7 3 2
The sum of the positive numbers is: 6

You might also like