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.
Here we are using task based approach. Each task will help you to understand the requirements and then you will be able to implement the code.
If Statement
If statement is used to check if a condition is true or false. If the condition is true, then the code inside the if statement is executed. If the condition is false, then the code inside the if statement is not executed.
Task
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 namedteaOrderof typestringand 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 theteaOrdervariable. - The
if(teaOrder == "Green Tea"){line starts anifstatement that checks if theteaOrdervariable 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 theifstatement. 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.
If-Else Statement
If-else statement is used to check if a condition is true or false. If the condition is true, then the code inside the if statement is executed. If the condition is false, then the code inside the else statement is executed.
Task
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 namedhourof typeintand 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 thehourvariable. - The
if(hour >= 8 && hour <= 18) {line starts anifstatement that checks if thehourvariable 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 anelseblock that is executed if theifstatement 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 theifstatement. 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.
Nested If-Else
Nested if-else consists of an if statement inside an else statement.
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 namedteaBagsof typeintand does not assign a value to it. - The
double pricePerBag, totalPrice, discount;line declares three variables namedpricePerBag,totalPrice, anddiscountof typedoubleand 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 theteaBagsvariable. - The
if (teaBags > 20) {line starts anifstatement that checks if theteaBagsvariable is greater than20. - The
discount = 0.20;line assigns the value0.20to thediscountvariable. - The
} else if (teaBags >= 10 && teaBags <= 20) {line starts anelse ifblock that is executed if theifstatement is false. - The
discount = 0.10;line assigns the value0.10to thediscountvariable. - The
} else {line starts anelseblock that is executed if theifstatement is false. - The
discount = 0;line assigns the value0to thediscountvariable. - The
}line ends theifstatement. - The
totalPrice = pricePerBag * teaBags;line calculates the total price by multiplying theteaBagsandpricePerBagvariables. - The
totalPrice -= (totalPrice * discount);line calculates the discounted price by subtracting thetotalPricemultiplied bydiscountfrom thetotalPricevariable. - The
cout << "Total price after discount is: " << totalPrice << endl;line prints the string “Total price after discount is: ” followed by the value of thetotalPricevariable to the console. return 0;line indicates that the program has finished executing and returns a value of 0.
Switch Case
Switch statement is used to check if a variable matches a specific value. If the variable matches the value, then the code inside the case statement is executed. If the variable does not match the value, then the code inside the default statement is executed.
It is useful when you have multiple options and want to execute different code based on the value of the variable.
Task
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 namedchoiceof typeintand does not assign a value to it. - The
double price;line declares a variable namedpriceof typedoubleand 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 thechoicevariable. - The
switch(choice){line starts aswitchstatement that checks the value of thechoicevariable. - The
case 1:line starts acaseblock that is executed if thechoicevariable is equal to1. - The
price = 2.0;line assigns the value2.0to thepricevariable. - The
cout << "You selected Green Tea. Price: "<< price << endl;line prints the string “You selected Green Tea. Price: ” followed by the value of thepricevariable to the console. - The
break;line ends thecaseblock. - The
case 2:line starts acaseblock that is executed if thechoicevariable is equal to2. - The
price = 3.0;line assigns the value3.0to thepricevariable. - The
cout << "You selected Lemon Tea. Price: "<< price << endl;line prints the string “You selected Lemon Tea. Price: ” followed by the value of thepricevariable to the console. - The
break;line ends thecaseblock. - The
case 3:line starts acaseblock that is executed if thechoicevariable is equal to3. - The
price = 4.0;line assigns the value4.0to thepricevariable. - The
cout << "You selected Black Tea. Price: "<< price << endl;line prints the string “You selected Black Tea. Price: ” followed by the value of thepricevariable to the console. - The
break;line ends thecaseblock. - The
default:line starts adefaultblock that is executed if thechoicevariable 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 thedefaultblock. - The
}line ends theswitchstatement. 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.
Start your journey with ChaiCode
All of our courses are available on chaicode.com. Feel free to check them out.