Hi folks, this is the second part of my series; “C++: Explain like I’m five”.
In this part, I will be explaining operators, user input, and if/else statements in detail.
So, fasten your seat belts, and let’s get started!
Operators in C++
An operator represents an action. For example “-” is an operator that represents subtraction just like “*” is an operator that represents multiplication. It works on two or more operands and produces an output. For example 1+2+3, here the “+” operator works on three operands and produces 6 as output.
Types of Operators in C++
There are 5 main types of operators used in C++ which are:
• Basic Arithmetic Operators
• Assignment Operators
• Auto-increment and Auto-decrement Operators
• Logical Operators
• Comparison (relational) operators
Now, let’s take a look at these operators in detail.
1. Basic Arithmetic Operators
Basic arithmetic operators are: +, -, *, /, %
• + is for addition
• - is for subtraction
• * is for multiplication
• / is for division
• % is for modulus (This operator return remainder; 20%5 would return 0)
Examples of Arithmetic operators are:
#include <iostream>
using namespace std;
int main(){
int numA = 240;
int numB = 40;
cout<<"numA + numB: "<<(numA + numB)<<endl;
cout<<"numA - numB: "<<(numA - numB)<<endl;
cout<<"numA * numB: "<<(numA * numB)<<endl;
cout<<"numA / numB: "<<(numA / numB)<<endl;
cout<<"numA % numB: "<<(numA % numB)<<endl;
return 0;
}
Output:
numA + numB: 280
numA - numB: 200
numA * numB: 9600
numA / numB: 6
numA % numB: 0
2. Assignment Operators
In C++, assignments operators are: =, +=, -=, *=, /=, %=
numB = numA would assign value of variable numA to the variable.
numB += numA is equal to numB = numB + numA
numB -= numA is equal to numB = numB - numA
numB *= numA is equal to numB = numB * numA
numB /= numA is equal to numB = numB/numA
numB %= numA is equal to numB = numB%numA
Example of Assignment Operators:
#include <iostream>
using namespace std;
int main(){
int numA = 240;
int numB = 40;
numB = numA;
cout<<"numB = numA: "<< numB<<endl;
numB += numA;
cout<<"numB + numA: "<<numB<<endl;
numB -= numA;
cout<<"numB - numA: "<<numB<<endl;
numB *= numA;
cout<<"numB * numA: "<<numB<<endl;
numB /= numA;
cout<<"numB / numA: "<<numB<<endl;
numB %= numA;
cout<<"numB % numA: "<<numB<<endl;
return 0;
}
Output:
numB = numA: 240
numB + numA: 480
numB - numA: 240
numB * numA: 57600
numB / numA: 240
numB % numA: 0
3. Auto-increment and Auto-decrement Operators
++ and -- are auto-increment and auto-decrement operators.
num++ equals num = num + 1
num-- equals num = num - 1
Example of Auto-increment and Auto-decrement Operators:
#include <iostream>
using namespace std;
int main(){
int numA = 240;
int numB = 40;
numA++; numB--;
cout<<"numA++ is: "<<numA<<endl;
cout<<"numB-- is: "<<numB;
return 0;
}
Output:
numA++ is: 241
numB-- is: 39
4. Logical Operators
Logical Operators are operators that are used with binary variables. They are mainly used in conditional statements and loops for evaluating any certain condition.
Logical operators in C++ are: "&&", "||" and "!"
Let’s say we have two boolean variables a1 and a2.
a1&&a2 will return true if both a1 and a2 are true, else it would return false.
a1||a2 will return false if both a1 and a2 are false, else it would return true.
!a1 would return the opposite of a1, which means that the output would be true if a1 is false and it would return false if a1 is true.
Example of Logical Operators:
#include <iostream>
using namespace std;
int main(){
bool a1 = true;
bool a2 = false;
cout<<"a1 && a2: "<<(a1&&a2)<<endl;
cout<<"a1 || b2: "<<(a1||a2)<<endl;
cout<<"!(a1 && a2): "<<!(a1&&a2);
return 0;
}
Output:
a1 && a2: 0
a1 || a2: 1
!(a1 && a2): 1
5. Relational operators
There are six relational operators in C++: ==, !=, >, <, >=, <=
• == returns true if both the left side and right side are equal
• != returns true if the left side is not equal to the right side of the operator
• > returns true if the left side is greater than the right
• < returns true if the left side is less than the right side
• >= returns true if the left side is greater than or equal to the right side
• <= returns true if the left side is less than or equal to the right side
Example of Relational operators:
#include <iostream>
using namespace std;
int main(){
int numA = 240;
int numB =40;
if (numA==numB) {
cout<<"numA and numB are equal"<<endl;
}
else{
cout<<"numA and numB are not equal"<<endl;
}
if( numA != numB ){
cout<<"numA and numB are not equal"<<endl;
}
else{
cout<<"numA and numB are equal"<<endl;
}
if( numA > numB ){
cout<<"numA is greater than numB"<<endl;
}
else{
cout<<"numA is not greater than numB"<<endl;
}
if( numA >= numB ){
cout<<"numA is greater than or equal to numB"<<endl;
}
else{
cout<<"numA is less than numB"<<endl;
}
if( numA < numB ){
cout<<"numA is less than numB"<<endl;
}
else{
cout<<"numA is not less than numB"<<endl;
}
if( numA <= numB){
cout<<"numA is less than or equal to numB"<<endl;
}
else{
cout<<"numA is greater than numB"<<endl;
}
return 0;
}
Output:
numA and numB are not equal
numA and numB are not equal
numA is greater than numB
numA is greater than or equal to numB
numA is not less than numB
numA is greater than numB
Before moving on to other topics, let's discuss how to take user input in C++.
User Input
As you all already know by now that "cout is" used to output (print) values. Here, we will use use cin to get user input. It is a predefined variable that reads data entered from the keyboard with the help of the extraction operator (>>).
Example:
Here, the user can input a number, which is then stored in the variable a. Then we will print the value of a:
#include <iostream>
using namespace std;
int main()
{
int a;
cout << "Type any integer: ";
cin >> a; // Getting user input from the keyboard
cout << "The entered integer is: " << a;
return 0;
}
Output:
Type any integer: 10
The entered integer is: 10
Since we have now covered all the introductory topics, let's move on to Flow Control(if/else statements, switch statements, loops, etc).
The first topic we will cover is if/else statements.
if/else statements
In if/else statements, we can use all the relational operators discussed above to perform different actions for different tasks. For a deeper understanding, take a look at the example of relational operators again.
The conditional statements in C++ are:
if: It is used to specify a block of code to be executed if a specified condition is true.
else: It is used to specify a block of code to be executed if the same condition is false.
else if: It is used to specify a new condition to test if the first condition is false.
switch: It is used to specify many alternative blocks of code to be executed.
(We will discuss switch statements in detail later.)
if statement
The if statement is used to specify a block of C++ code to be executed if a condition is true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example:
// Program to print only positive integers entered by the user
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter any integer: ";
cin >> num;
// to check if the entered number is positive
if (num > 0) {
cout << "You entered a positive integer: " << num<< endl;
}
cout << "This statement is always executed :)";
return 0;
}
Output:
Enter any integer: 10
You entered a positive integer: 10
This statement is always executed :)
Nested if statement
When there is an if statement inside another if statement, the resultant structure is called the nested if statement.
Syntax:
if(condition_A) {
Statement1(s);
if(condition_B) {
Statement2(s);
}
}
Example:
#include <iostream>
using namespace std;
int main(){
int a = 70;
/* This is a nested if statement as there is an if statement
inside another if body
*/
if( a < 100 ){
cout<<"Number is less than 100"<<endl;
if(a > 50){
cout<<"Number is greater than 50";
}
}
return 0;
}
Output:
Number is less than 100
Number is greater than 50
else statement
The else statement is used to specify a block of code to be executed if the condition is false.
Syntax:
if (condition) {
// This block of code will be executed if the condition is true
}
else {
// This block of code will be executed if the condition is false
}
Example:
#include <iostream>
using namespace std;
int main() {
int age = 25 ;
if (age < 18) {
cout << "Underage";
} else {
cout << "Adult";
}
return 0;
}
Output:
Adult
In the example above, age (25) is greater than 18, so the condition is false. Because of this, we then moved on to the else condition and printed to the screen "Adult". If the age was less than 18, the program would print "Underage".
Nested if/else statement
Just like in nested if statement, here too we add another if statement inside an if statement body. But we can also add else and else-if according to our necessity inside the second if statement. Moreover, we can nest multiple layers of if statements. This may seem complex but it is really easy. In order to understand this, let's take a look at the example.
Example:
/* This is a program to find if an integer is even or odd or neither (0)
using nested if statements */
#include <iostream>
using namespace std;
int main() {
int a;
cout << "Enter any integer: ";
cin >> a;
// Main if condition
if (a != 0) {
// inner if condition
if ((a % 2) == 0) {
cout << "The number is even!" << endl;
}
// inner else condition
else {
cout << "The number is odd!" << endl;
}
}
// Main else condition
else {
cout << "The number is 0 and it is neither even nor odd." << endl;
}
cout << "This statement is always executed!" << endl;
}
Output:
Enter any integer: 24
The number is even!
This statement is always executed!
else if statement
The else if statement is used to specify a new condition if the first condition is false. It is used to check multiple conditions.
Syntax:
if(condition_A) {
/*if condition_A is true, then execute this*/
statement(s);
}
else if(condition_B) {
/* execute this if condition_A is not met and
condition_B is met
*/
statement(s);
}
else if(condition_C) {
/* execute this if condition_A & condition_B are
not met and condition_C is met
*/
statement(s);
}
else {
/* if none of the above-mentioned conditions are true,
then these statements will get executed
*/
statement(s);
}
Example:
#include <iostream>
using namespace std;
int main(){
int num;
cout<<"Enter an integer number between 1 & 99999: ";
cin>>a;
if(num < 100 && a >= 1) {
cout<<"Its a two digit number";
}
else if(a < 1000 && a >= 100) {
cout<<"Its a three digit number";
}
else if(a < 10000 && a >= 1000) {
cout<<"Its a four digit number";
}
else if(a < 100000 && a >=10000) {
cout<<"Its a five digit number";
}
else {
cout<<"number is not between 1 & 99999";
}
return 0;
}
Output:
Enter an integer number between 1 & 99999: 1987
Its a four-digit number
That's all for today! All those who haven't read the first part of the series can do so from here. In the next blog, I will be explaining some other parts of flow control, so stay tuned ;)
Let's connect!
✨ Github
Top comments (1)
It represents multiplication and pointer dereference.
No;
+
works on one (for unary plus, e.g.,+1
) or two operands only. If you write1+2+3
, that's really(1+2)+3
: the first+
take the operands1
and2
, adds them, and stores the result in a temporary, sayT0
; the second+
takes the operandsT0
and3
and adds those. Speaking of()
, you don't mention anything about operator precedence, associativity, or how()
can be used to change the default.You left out pointer, array, and bit-wise operators in your list. You're also missing several assignment operators, e.g.,
<<=
. You also don't mention the difference between pre- and post-increment and decrement.There's no such thing as a "nested
if
statement". It's justif
statement [else
statement]. If either of those statements just so happen to be anotherif
, so be it. You also don't mention the common problem thatelse
binds to the most recentif
, nor how to solve that problem.I realize you're trying to present a simple introduction, but, well, 5-year-olds are just too young a target audience. Also, if you're going to omit things for simplicity, you should at least mention them in passing, then say "more on X later."
There are innumerable "Intro to C++" sites out there. It's not clear what unique perspective yours brings to the table.