0% found this document useful (0 votes)
5 views

Lab_4_3_Using_Nested_if_and_if_else_Statements

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab_4_3_Using_Nested_if_and_if_else_Statements

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

A sequence of if…else statements can accomplish the same task as a series of if


statements. However, a sequence of if…else statements is more efficient because once a
match has been found, the remainder of the tests are skipped. In a series of if statements,
all statements are evaluated. An alternative to writing nested if…else statements is to
write compound Boolean expressions.

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.

After completing this lab, you will be able to:


• Match the else statement with the appropriate if statement.
• Know when to nest and when not to nest if statements.
• Write code using nested ifs.
• Write compound Boolean expressions using if…else statements.
• Test all paths of your program.

Estimated completion time: 60–75 minutes


Lab 4.3 Steps: Using Nested if and if…else 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.

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 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.

Please enter 3 names: George Joe Adam


The alphabetical order of the names is:
Adam
George
Joe

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

You might also like