Else-If Statement Examples in C++


Description

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 Program

Here is the simple program for else-if statement in C++.

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

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

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

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.

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

Basic Programs


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



Learn All in Tamil © Designed & Developed By Tutor Joes