if else Statement in C++ Programming
Decision Making in C++
In this lesion, we will understand what is if and else statement, and how it works in a C++ program along with some example.
What is if else Statement
In C++ program the if statement alone tells us that if a condition is true then it will execute a block of statements and if the condition is false it won’t. But what-if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
if else Statement Syntax
if(condition)
{
/* Executes this block if
condition is true */
}
else
{
/* Executes this block if
condition is false */
}
In the above syntax inside the brackets ( ) of if statement we will write our condition. If the condition is true then the statements written within the curly braces { } of if statement will execute otherwise the statements written within the curly braces { } of else statement will execute.
Now let's see an example for more understanding.
Example
C++ program to check if an integer variable's value is even or odd number.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a=15;
if(a%2==0)
{
cout<<a<<" is an even number";
}
else
{
cout<<a<<" is an odd number";
}
return 0;
}
Output
15 is an odd number
Here you can see that the condition (a%2==0) is false because the remainder of the modulus division a%2 is not equal to 0. So the statement which is written inside the curly braces of else statement has executed and the output is printed on the screen.
Nested if Statement
In C++ program a nested if is an if statement which is inside the block of another if statement. Nested if statements means an if statement inside another if statement.
Nested if Statement Syntax
if(condition1)
{
// Executes when condition1 is true
if(condition2)
{
// Executes when condition2 is true
}
}
In the above syntax, you can see that we have written second if statement inside the curly braces { } of first if statement. If condition1 is true then condition2 will be checked, if condition2 is also true then the statement inside the curly braces { } of second if statement will execute.
Now let's see an example for more understanding.
Example
C++ program to check if an integer variable's value is even and is also greater than 20 using nested if statement.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a=24;
if(a%2==0)
{
if(a>20)
{
cout<<a<<" is an even number and is also greater than 20";
}
else
{
cout<<a<<" is an even number but not greater than 20";
}
}
else
{
cout<<a<<" is an odd number";
}
return 0;
}
Output
24 is an even number and is also greater than 20
Here you can see that the first condition (a%2==0) is true because the remainder of the modulus division (a%2) is equal to 0. So the second if statement which is written inside the curly braces { } of first if statement has executed and the second condition (a>20) is also true so statement which is written inside the curly braces { } of second if statement has executed and the output is printed on the screen.
Test Your Knowledge
Attempt the practical questions to check if the lesson is adequately clear to you.