Else-If Statement Examples in C++


An if statement can be followed by an optional else if...else statement, which is very usefull to test various conditions using single if...else if statement.When using if , else if , else statements there are few points to keep in mind.

  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of he remaining else if's or else's will be tested.

Example Programs

The program checks whether the integer variable num is less than 50 or not. If num is less than 50, the program outputs "Number is less than 50". Otherwise, it outputs "Number is greater than 50". The program initializes the integer variable num to 66.

Then, the program checks whether num is less than 50 using an if statement. Since num is greater than 50, the condition of the if statement is false, and the program proceeds to the else block. The else block contains the code that prints the message "Number is greater than 50" to the console using the cout function. Finally, the program terminates by executing the return 0; statement.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
   int num=66;
   if(num<50)
   {
      cout<<"\nNumber is less than 50";
   }
   else
   {
      cout<<"\nNumber is greater than 50";
   }
   return 0;
}
To download raw file Click Here

Output

Number is greater than 50

The program reads an integer number from the user and checks if the integer is positive or negative. The program starts by declaring an integer variable number. Then, the program prompts the user to enter an integer by printing the message "Enter an integer: " using cout. The user's input is then read and stored in number using the cin function.

After that, the program checks whether the value of number is greater than or equal to zero using an if statement. If the condition is true, then the program executes the block of code inside the if statement, which prints the message "You entered a positive integer: " followed by the value of number using cout.

If the value of number is less than zero, then the program skips the if block and proceeds to the else block. The else block contains the code that prints the message "You entered a negative integer: " followed by the value of number using cout. Finally, the program terminates by executing the return 0; statement.

Source Code Example : 2

#include <iostream>
using namespace std;
int main()
{
    int number;
    cout<<"\nEnter an integer: ";
    cin>>number;
    if(number>=0)
    {
        cout<<"\nYou entered a positive integer: "<<number<<endl;
    }
    else
    {
        cout<<"\nYou entered a negative integer: "<<number<<endl;
    }
    return 0;
}
To download raw file Click Here

Output

Enter an integer: 67
You entered a positive integer: 67

The program reads an integer number from the user and checks if the integer is positive. The program starts by declaring an integer variable number. Then, the program prompts the user to enter an integer by printing the message "Enter an integer: " using cout. The user's input is then read and stored in number using the cin function.

After that, the program checks whether the value of number is greater than zero using an if statement. If the condition is true, then the program executes the block of code inside the if statement, which prints the message "You entered a positive integer: " followed by the value of number using cout.

If the value of number is not greater than zero, then the program skips the if block and proceeds to the next line of code. Since there are no more lines of code after the if statement, the program terminates.

Source Code Example : 3

#include <iostream>
using namespace std;
int main()
{
    int number;
    cout<<"\nEnter an integer: ";
    cin>>number;
    if(number>0)
    {
        cout<<"\nYou entered a positive integer: "<<number<<endl;
    }
    return 0;
}
To download raw file Click Here

Output

Enter an integer: 69
You entered a positive integer: 69

The program is a simple demonstration of an if-else statement in C++. The program declares an integer variable time and initializes it to the value of 20. Then, the program checks if the value of time is less than 18 using an if statement. If the condition is true, then the program outputs the message "Good day." using cout.

However, since the value of time is 20, the condition in the if statement is false. Therefore, the program skips the if block and proceeds to the else block. The else block contains the code that outputs the message "Good evening." using cout.

Finally, the program terminates without returning any value. When we run this program, it will output the message "Good evening." since the value of time is greater than or equal to 18.

Source Code Example : 4

#include <iostream>
using namespace std;
int main()
{
    int time=20;
    if (time<18)
    {
        cout<<"\nGood day.";
    }
    else
    {
        cout<<"\nGood evening.";
    }
}
To download raw file Click Here

Output

Good evening.

The program compares two integer values x and y and outputs a message based on which value is greater. The program starts by declaring two integer variables x and y and initializing them to the values of 20 and 18 respectively.

Then, the program uses an if-else statement to compare the values of x and y. If the value of x is greater than the value of y, then the program outputs the message "x is greater than y" using cout.

If the value of y is greater than or equal to the value of x, then the program executes the code inside the else block, which outputs the message "y is greater than x" using cout.

In this case, the value of x is greater than the value of y, so the program will output the message "x is greater than y" when executed.

Source Code Example : 5

#include <iostream>
using namespace std;
int main()
{
    int x=20;
    int y=18;
    if(x>y)
    {
        cout<<"x is greater than y";
    }
    else
    {
        cout<<"y is greater than x";
    }
}
To download raw file Click Here

Output

x is greater than y

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples