if and if-else Statements
Learn about the different conditional statements in C++.
We'll cover the following
This lesson about conditional statements which are used to execute a certain statement or a block of code based on some condition(s).
Selection or conditional statements help create a flow in which we decide which set of statements will execute and which won’t, based on whether the condition is true or false.
The types of selection statements include:
- The
if
statement - The
if-else
statement - The
switch
statement
In this lesson, we’ll look at the if
and if-else
statements.
The if
statement (syntax)
The if
statement is the same as it is in the English language.
For example, if it’s raining, I will use an umbrella, otherwise I will not.
In C++, the if
statement is the same. If the condition is true
, then the statement inside the if
is executed. Otherwise it is not. Let’s see how to use the if
statement below:
if(condition)statement; // a statement ;/*In case of associating multiple statements inside an `if`*/if(condition){ // Adding a block statements1; // statement#1 or a block of statementss2; // statement#2 or a block of statements..}
Working of the if
statment.
See the animation below to understand how the if
statements works.
Now let’s write code using the if
statement.
#include<iostream> using namespace std; //example of if statement int main() { int a; cout << "Number: "; cin >> a; if(a < 30) { cout << "a is smaller than 30." << endl; } return 0; }
In line 9, we take a number as input from the user in variable a
.
In line 11, we check if the given input a
is less than 30
, and then execute all that is inside the curly brackets.
So if a
is indeed less than 30
, it prints a is smaller than 30
in line 13. Otherwise, if the condition is false
(i.e., a
is greater than 30
), then it simply runs what is after the if
statement, which is return 0
in this case.
Now, what if we had several if
conditions that we wanted to apply? In that case, the syntax would be:
if(condition 1)// statement or a block of statementsif(condition 2)// statement or a block of statementsif(condition 3)// statement or a block of statements
Let’s look at a code example now.
#include<iostream> using namespace std; //example of if statement(variant 2) int main() { int a; cout << "Number: "; cin >> a; if(a < 30) cout << "a is smaller than 30." << endl; if(a > 30) cout << "a is greater than 30." << endl; if(a == 30) cout << "a is equal to 30." << endl; return 0; }
The code above is an extended version of the previous example. In this example, we are checking whether a
is smaller, greater, or equal to 30
using three different if
conditions and statements.
If the input a
is less than 30
, then a is smaller than 30
is printed. If input a
is greater than 30, then a is greater than 30
is printed, and if input a
is equal to 30, then a is equal to 30
is printed. Depending on the value of a
, the respective if
statement is printed.
Also, note how we did not write any curly brakcets {}
after if
conditions, and the code still executed fine.
If we write
if
statements without the curly brackets{}
, only the immediate statement/expression after theif
condition is executed. Here, since we only need one statement afterif
to execute, the curly brakcets{}
are not needed. But to execute a block of statements, we would need to use the curly brackets{}
.
We do not put a semicolon
;
after theif
condition. If we put a;
, after the end ofif
condition, theif
condition would do nothing, and no statement would be associated with theif
statement. The block statement (the statements inside{...}
will be considered as separate independent statements not associated with theif
statement.
The if-else
statement (syntax)
We can write statements/expressions in the else
block that we would like to execute when the if
condition is not true.
Let’s see how to use the if-else
statement:
if(condition 1){// statement or a block of statements}else{// statement or a block of statements}
Suppose we had to write a program that would take five two-digit numbers from the user and display them on the console. However, the user could mistakenly enter a one-digit number or a three-digit number.
So we would need a condition to ensure that the user entered only a two-digit number. Only then will the program take the number and display it on the console. So for this situation, we can use an if-else
statement. We could add a condition that if
the number is a two-digit number, display the number; else
, the number entered is not a two-digit number as shown below.
#include <iostream>using namespace std;int main(){int num;cin >> num;if (num <= 99 && num >= 10)cout << num;elsecout << "The number entered is not a 2-digit number." << endl;return 0;}
Enter the input below
How if-else
works?
Let’s examine an example from the animation below to understand how it works.
In the animation above, we have a simple case where we print Pass
only if the score is greater than or equal to 50
and is less than or equal to max
(100
). Otherwise, we print Fail
in case the score is less than 50
.
Now let’s see a code example that uses an if-else
statement.
//example of if else statement(variant 1) #include<iostream> using namespace std; int main() { int num; cout << "Number: "; cin >> num; if(num > 0) cout << "number is positive." << endl; else cout << "number is negative" << endl; return 0; }
In this example, the if
statement is on line 11 with condition (num > 0
). If the number entered by the user is greater than 0
then the body of if
will be executed otherwise the body of else
will be executed.
Let’s see another example of the if-else
statement.
//example of if else statement(variant 2) #include<iostream> using namespace std; int main() { int num; cout << "Number: "; cin >> num; if(num > 10) cout << "Number is greater than 10." << endl; else if(num < 10) cout << "Number is less than 10." << endl; else if(num == 10) cout << "Number is equal to 10." << endl; else cout << "Invalid input." << endl; return 0; }
In this example, we have three conditions. The user will enter any integer in num
. If the input num
integer is greater than 10
, it will print Number is greater than 10.
. If the input num
integer is less than 10
, it will print Number is less than 10.
. Or if the input integer is equal to 10, it will execute the third if
part and print Number is equal to 10.
. Otherwise, the last else
will be executed, which will print Invalid input.
.