if-else

if, if...else and Nested if... else

Create decision making statements in a c++ program using different forms of if..else statement.

C++ if Statement

if(test expression)
{
    // statements
}
The if statement evaluates the test expression inside parenthesis.
If test expression is evaluated to true, statements inside the body of if is excuted.
If test expression is evaluated to false, statements inside the bocy of if is skipped.

How if statement works?

if statement expression

FlowChart of if Statement
flowchart of if statement

Example 1: C++ if statement

// program to print positive number enetered by the user
// If user enters negative number, it is skipped

#include 
using namespace std;
int main()
{
int number;
cout<<"Enter an integer : ";
cin>>number;

//check if the number is postive
if(number > 0)
{
cout<<"You entered a positive integer:"<<number;
}
cout<<"This statement is always executed.";
return 0;
}

Output

Enter an integer: 6
You entered a positive number: 6
This statement is always executed.

Output

Enter an integer: -6
This statement is always executed.

C++ if...else

The if else excuted the code inside the body of if statement if the test expression is true and skips the codes inside the body of else.
If the test expression is false, it excutes the codes inside the bocy of else statement and skips the codes inisde the body of if

How if...else Statement Works?

if else test expression
Flow chart of if...else statement

if else statement flowchart


Example 2: C++ if...else Statement
// Program to check whether an integer is positive or negative
// This program consider 0 as positive number

#includee
using namespace std;
int main()
{
int number;
cout<<"Enter an integer : ";
cin>>number;

//check if the number is postive
if(number >= 0)
{
cout<<"You entered a positive integer: "<<number<<endl;
}
else
{
cout<<"You entered a negative integer: "<<number<<endl;
}
cout<<"This line is always printed.";
return 0;
}

Output

Enter an integer: -3
You entered a negative integer:-3
This line is always printed.
The if...else statement excutes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made more than 2 possibilities.
The nested if...else statement allow you to check for multiple test expressions and execute different codes for more thatn two conditions.

C++ Nested if...else

if (testExpression1)
{
   //statements to be executed if testExpressions1 is true
}
else if(testExpression2)
{
   //statements to be executed if testExpression1 is false and testExpressions2 is executed
}
else if(testExpression 3)
{
   //statements to be executed if testExpression1 and testExpression2 is false then testExpression3 is executed
}
.
.
.
.
else
{
   // statements to be executed if all test expressions are false
}

Example 3: C++ Nested if...else

// Program to check whether an integer is positive, negative or zero 
#include
using namespace std;
int main()
{
    int number;
    cout<<"Enter an integer : ";
    cin>>number;

    if(number > 0)
    {
        cout<<"You entered a positive integer: "<<number<<endl;
    }
    else if(number < 0)
    {
        cout<<"You entered a negative integer: "<<number<<endl;  
    } 
    else
    {
        cout<<"You entered 0."<<endl;
    }
    cout<<"This line is always printed.";
    return 0;

}

Conditional/Ternary Operator ?:

A ternary operator on 3 operands which can be used instead of a if...else statement.
Consider this code:
 if (a < b)
 {
   a = b; 
 }
 else
 {
   a = -b;
 }
You can replace the above code with:
a = (a < b) ? b : -b;
The ternary operator is more readable than a if...else statement for short conditions.


Check out these examples to learn more:
Check Whether Number is Even or Odd Check 
Whether a character is Vowel or Constant
Find Largest Number Among Three Numbers