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

Lab_4_2_Using_the_Selection_Control_Structures_if_and_if_else

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)
1 views

Lab_4_2_Using_the_Selection_Control_Structures_if_and_if_else

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.

2 Using the Selection Control Structures if and if…else

There are three selection control structures in C++:

1. the if structure provides one alternative branch or path;


2. the if…else structure has two alternative branches or paths;
3. the switch structure allows multiple branches or paths.

Which path or branch the program will take in an if or if…else structure is determined by
evaluating a Boolean condition. The Boolean condition can be a simple Boolean expression
or a compound condition, that is, two or more Boolean expressions joined by logical
operators (the and or the or operators).

Objectives
In this lab, you will become acquainted with both the one-way alternative if selection
structure and the two-way alternative if…else selection structure. You will create a
program that uses the if and if…else structures.

After completing this lab you will be able to:


• Work with a one-way selection, which evaluates to either true or false and is
followed by one or more statements.
• Work with a two-way selection, which evaluates to either true or false and is
followed by one or more statements.
• Write a one-way selection structure followed by multiple statements.
• Write a two-way selection structure followed by one statement in each alternative.

Estimated completion time: 30–60 minutes


Lab 4.2 Steps: Using the Selection Control Structures if and
if…else
Write what the following statements display as output after the code executes. Use the
following assignment statement for Exercises 1 through 8:

int x = 6;

1. if (x > 10)
cout << “x is greater than 10\n”;
cout << “Selection allows decision making.\n”;

2. if (x = = 6)
cout << “A match is found.\n”;
cout << “Sequence continues after selection is
<< complete.\n”;

3. if (x < 8)
{
cout << “x is within the range.\n”;
cout << “This is a true statement.\n”;
}
cout << “And, after selection, the next statement is
<< executed.\n”;

4. if (x > 0 && x < 10)


cout << “x is in the range.\n”;
cout << “the value of x is “ << x;
5. if (x > 10)
cout << “x is greater than 10.\n”;
else
cout << “x is less than or equal to 10.\n”;
cout << “Selection allows decision making.\n”;

6. if (x < 8)
{
cout << “x is within the range.\n”;
cout << “This is a true statement.\n”;
}
else
{
cout << “x is out of range.\n”;
cout << “This is a false statement.\n”;
}
cout << “After selection, the next statement is
executed.\n”;

7. if (x >= 5)
{
}
else
cout << “Have a good day.\n”;

8. if (x > 0 && x < 10)


cout << “x is in range.\n”;
else
cout << “x is not in range.\n”;
9a. Critical Thinking Exercise: Design a program that will ask users to input their age and
whether or not they have a discount coupon. If the age is greater than 17, set the ticket
price to be eight dollars, and if the age is not greater than 17, set the ticket price to be
six dollars. If the user has a discount coupon, reduce the ticket price by 25 percent.
Output the ticket price. (Hint: use an integer variable for the price and a char variable
for the discount, i.e., ask the users to enter a y if they have a coupon and an n if they do
not.)

Write your design in the following space. For your design, write an algorithm (an English
language description of the steps required to successfully write the program).
9b. Write the source code for the design you created in Exercise 9a and name it tickets.cpp.

9c. Enter, compile, link, and execute tickets.cpp. Copy the output and save it in a block
comment at the end of your program. Save tickets.cpp in the Chap04 folder of your
Student Data Files.

The following is a copy of the screen results that might appear after running your
program, depending on the data you entered.

Enter your age: 24


Do you have a discount coupon? (enter a y for yes and an n
for no): y
Ticket Price: 6.00

Enter your age: 17


Do you have a discount coupon? (enter a y for yes and an n
for no): n
Ticket Price: 6.00

You might also like