C++ - Control Flow
Chapter 6: Control Flow
In this chapter, we will learn about control flow in C++. We will start by learning about conditional statements and how to use them. Then, we will learn about loops and how to use them. Finally, we will learn about switch statements and how to use them.
In this chapter, we will learn conditional statements via the task based approach. Each task will help you to understand the requirements and then you will be able to implement the code.
1. If Statement
Challenge:
Write a program that checks if the user wants to order Green Tea. If the user types “Green Tea,” the program should confirm their order.
Solution:
Going through the code:
- The
string teaOrder;
line declares a variable namedteaOrder
of typestring
and does not assign a value to it. - The
cout << "Enter your tea order";
line prints the string “Enter your tea order” to the console. - The
getline(cin, teaOrder);
line reads a line of input from the console and assigns it to theteaOrder
variable. - The
if(teaOrder == "Green Tea"){
line starts anif
statement that checks if theteaOrder
variable is equal to the string “Green Tea”. - The
cout << "You have ordered Green Tea" << endl;
line prints the string “You have ordered Green Tea” followed by a newline character to the console. - The
}
line ends theif
statement. return 0;
line indicates that the program has finished executing and returns a value of 0. It can return any value, but in this case, we are returning 0 as this is the exit code for a successful program execution.
2. If-Else Statement
Challenge:
Write a program that checks if a tea shop is open. If the current hour (input by the user) is between 8 AM and 6 PM, the shop is open; otherwise, it’s closed.
Solution:
Going through the code:
- The
int hour;
line declares a variable namedhour
of typeint
and does not assign a value to it. - The
cout << "Enter the current hour (0-23): ";
line prints the string “Enter the current hour (0-23): ” to the console. - The
cin >> hour;
line reads an integer from the console and assigns it to thehour
variable. - The
if(hour >= 8 && hour <= 18) {
line starts anif
statement that checks if thehour
variable is greater than or equal to 8 and less than or equal to 18. - The
cout << "Tea shop is OPEN!" << endl;
line prints the string “Tea shop is OPEN!” followed by a newline character to the console. - The
} else {
line starts anelse
block that is executed if theif
statement is false. - The
cout << "Tea shop is CLOSED!" << endl;
line prints the string “Tea shop is CLOSED!” followed by a newline character to the console. - The
}
line ends theif
statement. return 0;
line indicates that the program has finished executing and returns a value of 0. It can return any value, but in this case, we are returning 0 as this is the exit code for a successful program execution.
3. Nested If-Else
Challenge:
A tea shop offers discounts based on the number of tea cups ordered. Write a program that checks the number of cups ordered and applies a discount:* More than 20 cups:
- 20% discount
- Between 10 and 20 cups: 10% discount
- Less than 10 cups: No discount
Solution:
Going through the code
- The
int teaBags;
line declares a variable namedteaBags
of typeint
and does not assign a value to it. - The
double pricePerBag, totalPrice, discount;
line declares three variables namedpricePerBag
,totalPrice
, anddiscount
of typedouble
and does not assign a value to them. - The
cout << "Enter the number of tea bags you have: ";
line prints the string “Enter the number of tea bags you have: ” to the console. - The
cin >> teaBags;
line reads an integer from the console and assigns it to theteaBags
variable. - The
if (teaBags > 20) {
line starts anif
statement that checks if theteaBags
variable is greater than20
. - The
discount = 0.20;
line assigns the value0.20
to thediscount
variable. - The
} else if (teaBags >= 10 && teaBags <= 20) {
line starts anelse if
block that is executed if theif
statement is false. - The
discount = 0.10;
line assigns the value0.10
to thediscount
variable. - The
} else {
line starts anelse
block that is executed if theif
statement is false. - The
discount = 0;
line assigns the value0
to thediscount
variable. - The
}
line ends theif
statement. - The
totalPrice = pricePerBag * teaBags;
line calculates the total price by multiplying theteaBags
andpricePerBag
variables. - The
totalPrice -= (totalPrice * discount);
line calculates the discounted price by subtracting thetotalPrice
multiplied bydiscount
from thetotalPrice
variable. - The
cout << "Total price after discount is: " << totalPrice << endl;
line prints the string “Total price after discount is: ” followed by the value of thetotalPrice
variable to the console. return 0;
line indicates that the program has finished executing and returns a value of 0.
4. Switch Case
Challenge:
Write a program that lets the user select a tea type from a menu. Use a switch statement to display the price based on the selected tea:
- Green Tea: $2
- Black Tea: $3
- Oolong Tea: $4
Solution:
Going through the code:
- The
int choice;
line declares a variable namedchoice
of typeint
and does not assign a value to it. - The
double price;
line declares a variable namedprice
of typedouble
and does not assign a value to it. - The
cout << "Select your tea\n";
line prints the string “Select your tea\n” to the console. - The
cout << "1. Green Tea\n";
line prints the string “1. Green Tea\n” to the console. - The
cout << "2. Lemon Tea\n";
line prints the string “2. Lemon Tea\n” to the console. - The
cout << "3. Oolong Tea\n";
line prints the string “3. Oolong Tea\n” to the console. - The
cout << "Enter your choice in number: \n";
line prints the string “Enter your choice in number: \n” to the console. - The
cin >> choice;
line reads an integer from the console and assigns it to thechoice
variable. - The
switch(choice){
line starts aswitch
statement that checks the value of thechoice
variable. - The
case 1:
line starts acase
block that is executed if thechoice
variable is equal to1
. - The
price = 2.0;
line assigns the value2.0
to theprice
variable. - The
cout << "You selected Green Tea. Price: "<< price << endl;
line prints the string “You selected Green Tea. Price: ” followed by the value of theprice
variable to the console. - The
break;
line ends thecase
block. - The
case 2:
line starts acase
block that is executed if thechoice
variable is equal to2
. - The
price = 3.0;
line assigns the value3.0
to theprice
variable. - The
cout << "You selected Lemon Tea. Price: "<< price << endl;
line prints the string “You selected Lemon Tea. Price: ” followed by the value of theprice
variable to the console. - The
break;
line ends thecase
block. - The
case 3:
line starts acase
block that is executed if thechoice
variable is equal to3
. - The
price = 4.0;
line assigns the value4.0
to theprice
variable. - The
cout << "You selected Black Tea. Price: "<< price << endl;
line prints the string “You selected Black Tea. Price: ” followed by the value of theprice
variable to the console. - The
break;
line ends thecase
block. - The
default:
line starts adefault
block that is executed if thechoice
variable is not equal to1
,2
, or3
. - The
cout << "Invalid choice" << endl;
line prints the string “Invalid choice” followed by a newline character to the console. - The
break;
line ends thedefault
block. - The
}
line ends theswitch
statement. return 0;
line indicates that the program has finished executing and returns a value of 0.
Summary
In this chapter, we have learned about conditional statements and how to use them. We have also learned about loops and how to use them. We have also learned about switch statements and how to use them.