Skip to content

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.

Control Flow

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:

#include <iostream>
#include <string>
using namespace std;
int main(){
string teaOrder;
cout << "Enter your tea order";
getline(cin, teaOrder);
if(teaOrder == "Green Tea"){
cout << "You have ordered Green Tea" << endl;
}
return 0;
}

Going through the code:

  • The string teaOrder; line declares a variable named teaOrder of type string 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 the teaOrder variable.
  • The if(teaOrder == "Green Tea"){ line starts an if statement that checks if the teaOrder 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 the if 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:

#include <iostream>
using namespace std;
int main(){
int hour;
cout << "Enter the current hour (0-23): ";
cin >> hour;
if(hour >= 8 && hour <= 18) {
cout << "Tea shop is OPEN!" << endl;
} else {
cout << "Tea shop is CLOSED!" << endl;
}
return 0;
}

Going through the code:

  • The int hour; line declares a variable named hour of type int 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 the hour variable.
  • The if(hour >= 8 && hour <= 18) { line starts an if statement that checks if the hour 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 an else block that is executed if the if 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 the if 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:

#include <iostream>
using namespace std;
int main(){
int cups;
double pricePerCup = 2.5, totalPrice, discount;
cout << "Enter the number of tea cups" ;
cin >> cups;
totalPrice = pricePerCup * cups;
if(cups > 20){
discount = 0.20;
}else if (cups >= 10 && cups <= 20){
discount = 0.10;
} else {
discount = 0;
}
totalPrice -= (totalPrice * discount);
cout << "Total price after discount is: " << totalPrice << endl;
return 0;
}

Going through the code

  • The int teaBags; line declares a variable named teaBags of type int and does not assign a value to it.
  • The double pricePerBag, totalPrice, discount; line declares three variables named pricePerBag, totalPrice, and discount of type double 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 the teaBags variable.
  • The if (teaBags > 20) { line starts an if statement that checks if the teaBags variable is greater than 20.
  • The discount = 0.20; line assigns the value 0.20 to the discount variable.
  • The } else if (teaBags >= 10 && teaBags <= 20) { line starts an else if block that is executed if the if statement is false.
  • The discount = 0.10; line assigns the value 0.10 to the discount variable.
  • The } else { line starts an else block that is executed if the if statement is false.
  • The discount = 0; line assigns the value 0 to the discount variable.
  • The } line ends the if statement.
  • The totalPrice = pricePerBag * teaBags; line calculates the total price by multiplying the teaBags and pricePerBag variables.
  • The totalPrice -= (totalPrice * discount); line calculates the discounted price by subtracting the totalPrice multiplied by discount from the totalPrice variable.
  • The cout << "Total price after discount is: " << totalPrice << endl; line prints the string “Total price after discount is: ” followed by the value of the totalPrice 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:

#include <iostream>
using namespace std;
int main(){
int choice;
double price;
cout << "Select your tea\n";
cout << "1. Green Tea\n";
cout << "2. Lemon Tea\n";
cout << "3. Oolong Tea\n";
cout << "Enter your choice in number: \n";
cin >> choice;
switch(choice){
case 1:
price = 2.0;
cout << "You selected Green Tea. Price: "<< price << endl;
break;
case 2:
price = 3.0;
cout << "You selected Lemon Tea. Price: "<< price << endl;
break;
case 3:
price = 4.0;
cout << "You selected Oolong Tea. Price: "<< price << endl;
break;
default:
cout << "Invalid choice" << endl;
break;
}
return 0;
}

Going through the code:

  • The int choice; line declares a variable named choice of type int and does not assign a value to it.
  • The double price; line declares a variable named price of type double 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 the choice variable.
  • The switch(choice){ line starts a switch statement that checks the value of the choice variable.
  • The case 1: line starts a case block that is executed if the choice variable is equal to 1.
  • The price = 2.0; line assigns the value 2.0 to the price variable.
  • The cout << "You selected Green Tea. Price: "<< price << endl; line prints the string “You selected Green Tea. Price: ” followed by the value of the price variable to the console.
  • The break; line ends the case block.
  • The case 2: line starts a case block that is executed if the choice variable is equal to 2.
  • The price = 3.0; line assigns the value 3.0 to the price variable.
  • The cout << "You selected Lemon Tea. Price: "<< price << endl; line prints the string “You selected Lemon Tea. Price: ” followed by the value of the price variable to the console.
  • The break; line ends the case block.
  • The case 3: line starts a case block that is executed if the choice variable is equal to 3.
  • The price = 4.0; line assigns the value 4.0 to the price variable.
  • The cout << "You selected Black Tea. Price: "<< price << endl; line prints the string “You selected Black Tea. Price: ” followed by the value of the price variable to the console.
  • The break; line ends the case block.
  • The default: line starts a default block that is executed if the choice variable is not equal to 1, 2, or 3.
  • The cout << "Invalid choice" << endl; line prints the string “Invalid choice” followed by a newline character to the console.
  • The break; line ends the default block.
  • The } line ends the switch 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.