Operators
In this chapter, we will learn about operators in C++. We will start by learning about the different types of operators in C++ and how to use them. Operators are used to perform operations on variables and values.
Types of Operators
C++ provides several types of operators that are used to perform operations on variables and values. Some of the types of operators in C++ include:
- Arithmetic Operators: These are operators that are used to perform arithmetic operations on variables and values.
- Assignment Operators: These are operators that are used to assign values to variables.
- Comparison Operators: These are operators that are used to compare values and determine if they are equal or not.
- Logical Operators: These are operators that are used to perform logical operations on variables and values.
- Increment and Decrement Operators: These are operators that are used to increment or decrement the value of a variable.
- Bitwise Operators: These are operators that are used to perform bitwise operations on variables and values.
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and values.
Task
Create a program that calculates the total price of tea cups. The user inputs the number of cups they want and the price per cup. The program should calculate the total price, apply a 5% discount if the total is above a certain amount, and show the final price.
Solution
#include <iostream>using namespace std;
int main(){ int cups; double pricePerCup, totalPrice, discountedPrice;
cout << "Enter the number of tea cups: "; cin >> cups; cout << "Enter the price per cups: "; cin >> pricePerCup;
totalPrice = cups * pricePerCup;
// apply 5% discount if total price is above 100 if (totalPrice > 100) { discountedPrice = totalPrice - (totalPrice * 0.05); cout << "Discounted price is: " << discountedPrice << endl; } else { cout << "Total price is " << totalPrice << endl; }
return 0;}Going through the code:
- The
int cups;line declares a variable namedcupsof typeintand does not assign a value to it. - The
double pricePerCup, totalPrice, discountedPrice;line declares three variables namedpricePerCup,totalPrice, anddiscountedPriceof typedoubleand does not assign a value to them. - The
cout << "Enter the number of tea cups: ";line prints the string “Enter the number of tea cups: ” to the console. - The
cin >> cups;line reads an integer from the console and assigns it to thecupsvariable. - The
cout << "Enter the price per cups: ";line prints the string “Enter the price per cups: ” to the console. - The
cin >> pricePerCup;line reads a double from the console and assigns it to thepricePerCupvariable. - The
totalPrice = cups * pricePerCup;line calculates the total price by multiplying thecupsandpricePerCupvariables. - The
if (totalPrice > 100) {line starts anifstatement that checks if thetotalPricevariable is greater than100. - The
discountedPrice = totalPrice - (totalPrice * 0.05);line calculates the discounted price by subtracting thetotalPricemultiplied by0.05from thetotalPricevariable. - The
cout << "Discounted price is: " << discountedPrice << endl;line prints the string “Discounted price is: ” followed by the value of thediscountedPricevariable to the console. - The
} else {line starts anelseblock that is executed if theifstatement is false. - The
cout << "Total price is " << totalPrice << endl;line prints the string “Total price is ” followed by the value of thetotalPricevariable to the console. - The
}line ends theifstatement.
Assignment Operators
Assignment operators are used to assign values to variables.
Task
Write a program that allows a user to input the number of tea bags they have. Assign additional bags to them based on certain conditions (e.g., if they have fewer than 10 bags, give them 5 extra). Update the original number using assignment operators.
Solution
#include <iostream>using namespace std;
int main(){ int teaBags;
cout << "Enter the number of tea bags you have: "; cin >> teaBags;
if (teaBags < 10) { // teaBags = teaBags + 5 teaBags += 5; } cout << "Your total bags are: " << teaBags;
return 0;}Going through the code:
- The
int teaBags;line declares a variable namedteaBagsof typeintand does not assign a value to it. - 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 < 10) {line starts anifstatement that checks if theteaBagsvariable is less than10. - The
// teaBags = teaBags + 5line adds5to theteaBagsvariable. - The
teaBags += 5;line adds5to theteaBagsvariable. - The
}line ends theifstatement. - The
cout << "Your total bags are: " << teaBags;line prints the string “Your total bags are: ” followed by the value of theteaBagsvariable to the console.
Relational Operators
Relational operators are used to compare values and determine if they are equal or not.
Task
A tea shop offers a loyalty program. Customers who buy more than 20 cups of tea get a special “Gold” badge, and those who buy 10 to 20 cups get a “Silver” badge. Write a program to display the badge they will receive based on the number of cups they buy.
Solution
#include <iostream>using namespace std;
int main(){ int cups;
cout << "Enter the number of cups you have"; cin >> cups;
if (cups > 20) { cout << "You will get a GOLD badge" << endl; } else if (cups >= 10 && cups <= 20) { cout << "You will get a SILVER badge" << endl; } else { cout << "NO BADGE for you" << endl; }
return 0;}Going through the code
- The
int cups;line declares a variable namedcupsof typeintand does not assign a value to it. - The
cout << "Enter the number of cups you have";line prints the string “Enter the number of cups you have” to the console. - The
cin >> cups;line reads an integer from the console and assigns it to thecupsvariable. - The
if (cups > 20) {line starts anifstatement that checks if thecupsvariable is greater than20. - The
cout << "You will get a GOLD badge" << endl;line prints the string “You will get a GOLD badge” followed by a newline character to the console. - The
} else if (cups >= 10 && cups <= 20) {line starts anelse ifblock that is executed if theifstatement is false. - The
cout << "You will get a SILVER badge" << endl;line prints the string “You will get a SILVER badge” followed by a newline character to the console. - The
} else {line starts anelseblock that is executed if theifstatement is false. - The
cout << "NO BADGE for you" << endl;line prints the string “NO BADGE for you” followed by a newline character to the console. - The
}line ends theifstatement.
Logical Operators
Logical operators are used to perform logical operations on variables and values.
Task
Create a program that checks if a user is eligible for a tea subscription discount. The discount applies if the user is either a student or has purchased more than 15 cups. Ask the user to input their status (student or not) and their cup count.
Solution
#include <iostream>using namespace std;
int main(){ bool isStudent; int cups;
cout << "Are you a student (1 for yes and 0 for No) ?"; cin >> isStudent; cout << "How many cups of tea have you purchased ?"; cin >> cups;
if (isStudent || cups > 15) { cout << "You are elegible for a discount " << endl; } else { cout << "You are NOT elegible for a discount " << endl; }
return 0;}Going through the code
- The
bool isStudent;line declares a variable namedisStudentof typebooland does not assign a value to it. - The
int cups;line declares a variable namedcupsof typeintand does not assign a value to it. - The
cout << "Are you a student (1 for yes and 0 for No) ?";line prints the string “Are you a student (1 for yes and 0 for No) ?” to the console. - The
cin >> isStudent;line reads a boolean value from the console and assigns it to theisStudentvariable. - The
cout << "How many cups of tea have you purchased ?";line prints the string “How many cups of tea have you purchased ?” to the console. - The
cin >> cups;line reads an integer from the console and assigns it to thecupsvariable. - The
if (isStudent || cups > 15) {line starts anifstatement that checks if theisStudentvariable is true or if thecupsvariable is greater than15. - The
cout << "You are elegible for a discount " << endl;line prints the string “You are elegible for a discount ” followed by a newline character to the console. - The
} else {line starts anelseblock that is executed if theifstatement is false. - The
cout << "You are NOT elegible for a discount " << endl;line prints the string “You are NOT elegible for a discount ” followed by a newline character to the console. - The
}line ends theifstatement.
Bitwise Operators
Bitwise operators are used to perform bitwise operations on variables and values. Bitwise operations are used to manipulate individual bits of a binary number. Decimal numbers are represented using binary numbers, which are made up of 1s and 0s. Bitwise operators are used to perform operations on these binary numbers.
You can learn more about bitwise operators by visiting Bitwisecmd.com. We talked about it in the video, watch it for more information.
Summary
In this chapter, we have learned about operators in C++. We have also learned about the different types of operators and how to use them. By the end of this chapter, you should have a good understanding of how to use operators in C++.
Start your journey with ChaiCode
All of our courses are available on chaicode.com. Feel free to check them out.