C++ Else If - C++ else

Hey! today, students, we will learn C++ Else and separate C++ Else If topics. So, this course will be complete topics 2020.

The else statement is using to specify the block of code when the code is executing.

if the condition is false

if (condition)
{
    // This block of code is running when the condition is true
}
else
{
    // This block of code is running when the condition is false
}

So, in the next example, we with do an example which we validate the employ age limit in condition with if else statements.

#include <iostream>
using namespace std;

int main()
{
    int employeeAge = 22;
    if(employeeAge < 20)
  {
     cout << "Employee is not Eligible";
  }
    else
  {
     cout << "Employee is Eligible";
  }
     return 0;
}

So, the above example’ output is:

Employee is Eligible

The explanation of Example:

In the example above, employeeAge(22) is greater than 20, so the condition is false. if the employeeAge was less than 20, the program print the ‘Employee is not eligible’. But the age is greater than 20 and the condition is false, so It prints the ‘Employee is Eligible’.

C++ Else If

The use of else if statement is specifying a new condition, if the first condition is false.

Remember: Mostly we are using C++ else if statement when we have to check more than 1 condition.

So, the Syntax:

if (conditionOne)
{
    // This block of code is running when the conditionOne is true
}
else if(ConditionTwo)
{
    // This block of code is running when the conditionOne is false and ConditionTwo is true
}
else 
{
    // This block of code is running when the conditionOne is false and ConditionTwo is false
}

Then the Example is:

#include <iostream>
using namespace std;

int main()
{
    int employeeAge = 20;
    if(employeeAge < 18)
  {
     cout << "Employee is not Eligible";
  }
    else if(employeeAge == 20) 
  {
     cout << "The employee's Age has a perfect match, he is totally Eligible";
  }
    else
  {
     cout << "Employee is Eligible";
  }
     return 0;
}

The explanation of Example 2:

So, In the above example, we did practice with else if and check more than one condition, the list is below of explanation.

  • In the first, we declare a variable integer data type employee Age with value 22
  • So, we check the first condition which writes that if an employee age is less than 18
  • Then, print the Employee is not Eligible
  • if the first condition false. then moving on the second condition with the help of else statement
  • So, the second condition is checking that if the age of an employee is equal to 20 (if this statement is true)
  • Then it will print the “The employee’s Age has a perfect match, he is totally Eligible
  • If the second condition is false. then the program flow will move toward the else statement
  • And the last else statement will print the value: Employee is Eligible

So, Our Program will returns:

The employee's Age has a perfect match, he is totally Eligible

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *