C++ if-else-if Statements with examples

- February 26, 2018
The if-else-if structure is also known as nested if else structure or multiple if else structure. It is used to execute one block of statements from multiple blocks of statements.
In C++ if else if structure, multiple conditions and multiple blocks of statements are given. When any of the given condition is true, the statements associated with that condition are executed. In this case, all other blocks are ignored.
The general syntax of "if-else-if" structure is as follows:
if (condition-1)
      { Block-1 }
else if (condition-2)
      { Block-2 }
else if (condition-3)
      { Block-3 }
----------------
----------------
else if (condition-m)
      { Block-m }
   else
      { Block-n}
 The use of last condition (else part condition-m) is optional.

Flowchart of if else if Structure

Following is the flowchart:
Process of if else if in C++

Execution of if else if statements

In C++, the if else if structure is executed by following these rules:

  • The conditions are evaluated in the given sequence until a true condition is found.
  • The statements associated with true condition are executed.
  • The block of statements of following the last else is executed if all conditions are false.

Example Programs

Example 1: The following program finds out whether a number is positive, negative or zero by using C++ if-else-if statement.
#include<iostream>
using namespace std;
main()
 {
   int num;
   cout<<"Enter Number:";
   cin>>num;
   if (num >0 )
      cout<<"Entered Number is Positive";
   else if (num < 0)
      cout<<"Entered Number is Negative";
   else
      cout<<"The Number is Zero";
   return 0;
 }
In the above source code, "if-else-if" statement is used. Suppose the user enters a positive number, the number is tested with if statement.  The answer is true because number is positive. The message is displayed on the screen and rest of the conditions will be ignored.

Example 2: The following source code of the program inputs any character through keyboard and determines whether it is a capital letter, small letter, a digit or a special character symbol using logical operators and "if-else-if" statement.
#include<iostream>
using namespace std;
main()
 {
   char ch;
   cout<<"Enter any character:";
   cin>>ch;
   if (ch>='A' && ch <= 'Z')
      cout<<"It is a capital letter":
   else if (ch>= 'a' && ch<='z')
      cout<<"It is a small letter";
   else if (ch>= '0' && ch <='9')
      cout<<"It is a digit";
   else
      cout<<"It is a special character";
   getch();
 }
Example 3: The following source code inputs three numbers and finds out the smallest number using logical operators and "if-else-if" statement.
#include<iostream>
using namespace std;
main()
 {
   int num1, num2, num3, mini;
   cout<<"Enter First Number:";
   cin>>num1;
   cout<<"Enter Second Number:";
   cin>>num2;
   cout<<"Enter Third Number:";
   cin>>num3;
   if (num1 < num2 && num1 < num3)
      mini = num1;
   else if (num2 < num1 && num2 < num3)
      mini = num2;
   else
      mini = num3
   cout<<"The Minimum Number is "<<mini;
   return 0;
 }
Example 4: The following source code of program performs simple arithmetic calculations using "if-else-if" statement.
#include<iostream>
using namespace std;
main()
 {
   float n, m; char opt;
   cout<<"Enter first value, operator and second value:"<<endl;
   cin>>n>>opt>>m;
   if (opt == '-')
      cout<<n-m;
   else if (opt == '*')
      cout<<n*m;
   else if (opt == '/')
      cout<<n/m;
   else
      cout<<"Invalid operator";
   return 0;
 }
Output of the program:
Enter first value, operator and second value:
4+6


10