If-Else-If Statement Examples in C++


The if else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. An if statement executes code conditionally depending on the result of the condition in parentheses.

When condition in parentheses is true it will enter to the block of if statement which is defined by curly braces like open braces and close braces. Opening bracket till the closing bracket is the scope of the if statement. The else block is optional and can be omitted. It runs if the if statement is false and does not run if the if statement is true because in that case if statement executes.

 Syntax :
   if ( condition )
   {
       // block of statement to be executed if condition is true ;
   }
   else
   {
       block of statement to be executed if condition is false ;
   }

Example Program

The program takes an integer input from the user and determines the number of digits in the input number.

  • The program starts by declaring an integer variable no and prompting the user to enter an integer between 1 and 99999 using cout and cin.
  • Then, the program uses a series of if-else statements to check the range of the input number and determine the number of digits.
  • If the input number is between 1 and 99, the program outputs the message "Its a two digit number". If the input number is between 100 and 999, the program outputs the message "Its a three digit number". If the input number is between 1000 and 9999, the program outputs the message "Its a four digit number". If the input number is between 10000 and 99999, the program outputs the message "Its a five digit number".
  • If the input number is not within the specified range, the program outputs the message "Number is not between 1 & 99999".
  • For example, if the user enters the input number 12345, the program will output the message "Its a five digit number".
  • Therefore, the output of the program will depend on the user's input.

Source Code Example : 1

#include <iostream>
using namespace std;
int main()
{
    int no;
    cout<<"\nEnter an integer number between 1 & 99999: ";
    cin>>no;
    if(no<100 && no>=1)
    {
      cout<<"\nIts a two digit number";
    }
    else if(no<1000 && no>=100) {
      cout<<"\nIts a three digit number";
    }
    else if(no<10000 && no>=1000)
    {
      cout<<"\nIts a four digit number";
    }
    else if(no<100000 && no>=10000)
    {
      cout<<"\nIts a five digit number";
    }
    else
    {
      cout<<"\nNumber is not between 1 & 99999";
    }
    return 0;
}
To download raw file Click Here

Output

Enter an integer number between 1 & 99999: 56
Its a two digit number

The program uses an if-else statement to print a greeting message based on the value of the integer variable time. The program starts by declaring an integer variable time and initializing it to 22.

Then, the program uses an if-else statement to check the value of time. If time is less than 10, the program outputs the message "Good morning." . If time is less than 20, the program outputs the message "Good day." . If time is greater than or equal to 20, the program outputs the message "Good evening.". Since time is initialized to 22, it is greater than 10 and 20, so the program will output the message "Good evening." .

Source Code Example : 2

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

Output

Good evening.

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