Lab_4_3_Using_Nested_if_and_if_else_Statements
Lab_4_3_Using_Nested_if_and_if_else_Statements
The statement(s) executed in a selection can be any valid C++ statement(s). This includes an
if statement located within another if or if…else statement. These statements are
called nested statements. When the selection requires more than one alternative, use the
nested if statement.
An else statement is not required; however, when used, all else statements must be
paired with an if. In a nested if statement, C++ associates an else with the most recent
incomplete if structure—that is, the most recent if that has not been paired with an
else. The if and if…else structures control only one statement at a time. To permit
more complex statements, C++ provides a structure called a compound statement or a block
of statements. Some problems require the implementation of more than two alternatives.
When one control statement is located within another, it is said to be nested.
One method of program development is an informal mixture of C++ and ordinary language
called pseudocode or pseudo. To find logical errors in your pseudo, you should always use a
wide range of values in a walk-through to evaluate the code under as many different
circumstances as possible. Once you have written your program, you should also test the
program using data values that test all program paths.
Objectives
In this lab, you evaluate Boolean expressions in nested if statements.
Write what the following statements display as output after the program executes. Then
design and write a program that uses nested if and if…else statements.
1a. Write what the following statements display as output after the program executes.
int temperature = 78;
int month = 6;
string name = “Pat Boone”;
if (temperature >= 70 && month >=6)
cout << “Wear white shoes.\n”;
else if (name == “Pat Boone”)
cout << “Wear white shoes.\n”;
else
cout << “Wear black shoes.\n”;
1b. What is the output of the program in Exercise 1a when temperature = 70, month = 5,
and name = “Pat Boone”?
1c. What is the output of the program in Exercise 1a when temperature = 60, month = 5,
and name = “Pat Boone”?
1d. What is the output of the program in Exercise 1a when temperature = 60, month = 5,
and name = “Your name”?
2a. Design a program that prompts the user to enter three names separated by spaces.
Using compound and nested if statements, display the names in alphabetical order.
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
nameSort.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 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 using the following data: George Joe Adam
2c. Enter, compile, link, and execute nameSort.cpp. Then copy the output and save it in a
block comment at the end of your program. Save nameSort.cpp in the Chap04 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.
2d. Rerun nameSort.cpp with the names entered in this order: Joe Adam George
2e. Rerun nameSort.cpp with the names entered in this order: Adam Joe George
2f. Rerun nameSort.cpp with the names entered in this order: George Adam Joe