# If Else Statement

0 brigettefults72 November 13, 2020

If we have to execute a block of statements when a particular condition is met or not met. This is known as Decision making. For decision making, We have 4 types of control statements in C++.

• if statement
• if….else statement
• else…..if statement
• nested if….else statement

## 1. IF Statement:

If the statement is a powerful decision-making statement and is used to control the flow of execution of statements. It basically a two-way decision statement and is used together with an expression, i.e. test condition. The if statement evaluates the expression first and then, if the value the expression is true, it executes the statement within the block. Otherwise, it skips the statements within its block and continues from the first statement outside the if block. It takes the form,

``````if(condition)
{
statement-block;
}
statement-x;``````

### Flow Diagram

Let’s take an example to make clear concepts on IF-ELSE.

``````#include <iostream>
using namespace std;

int main(){

int num = 10;

if(num % 2 == 0){
cout << "Number is Even" << endl;
}

return 0;
}``````

The output of the above program is:

``Number is Even``

## 2. IF…ELSE Statement

Theif…else statement is an extension of the simpleifstatement. It is used when there are two possible actions – one when a condition is true, and the other when it is false. The general form is

``````if(condition)
{
true_block statement;
}
else
{
false_block statement;
}
statement-x;``````

### Flowchart

Let’s look upon one example to make clear concepts on if..else condition.

``````#include <iostream>
using namespace std;

int main(){

int num = 10;

if(num % 2 == 0)
{
cout << "Number is Even" << endl;
}
else
{
cout << "Number is Odd" << endl;
}

return 0;
}``````

The output of the above program is:

``Number is Even``

## 3. ELSE…IF Statement

The else if the statement is used when there are more than two possible actions depending upon the outcome of the test. When an action is taken, no others can be executed or taken. In such a situation, the if…else if…else if….else statement is used. This structure takes the form,

``````if(condition-1)
{
statement-1;
}
else if(condition-2)
{
statement-2;
}
else if(condition-3)
{
statement-3;
}
else
{
default-statement;
}``````

Let’s look at one example to make clear concepts on else…if condition.

``````#include <iostream>
using namespace std;

int main(){

int num = 15;

if(num % 2 == 0)
{
cout << "Number is Even" << endl;
}
else if(num % 3 == 0)
{
cout << "Number is Odd" << endl;
}
else
{
cout << "Invalid Number" << endl;
}

return 0;
}``````

The output of the above program is:

``Number is Odd``

## 4. Nested IF…ELSE Statement

Nested IF…ELSE statement means there is anIF condition inside the if condition. The syntax looks like this.

``````if(condition)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}``````

Let’s look at examples to make clear concepts on nested if…else condition.

``````#include <iostream>
using namespace std;

int main(){

int num = 15;

if(num % 2 == 0)
{
if(num > 5 == 0)
{
cout << "Number is greater than 5 and Even" << endl;
}else{
cout << "Number is less than 5 and Even" << endl;
}
}
else
{
cout << "Number is Odd" << endl;
}

return 0;
}``````

The output of the above program is:

``Number is greater than 5 and Even``