"Nested-if-else" statements c++

"Nested-if-else" statements c++

When an "if-else" structure is placed in another "if-else" structure, it is called "nested-if-else" structure. It is used for multiple selection.

Its general syntax is:

if (condition-1)
     statement-1;
else if (condition-2)
    statement-2;
else if (condition-3)
    statement-3;
--------------------------------
--------------------------------
else if (condition-m)
    statement-m
else
    statement-n;


nested-if-else flowchart


Example:
     Write a program to perform simple arithmetic operation by using "nested-if-else" structure.

#include <iostream.h>
main()
{
int a,b;
char op;
cout <<"Enter 1st integer, operator & 2nd integer\n";
cout << "Press Enter Key";
cin >>a>>op>>b;
       if (op=='+')
             cout  << "Addition = " <<(a+b);
       else if (op=='-')
             cout  <<  "Subtraction=" <<(a-b);
       else if (op=='/')
             cout  <<  "Division=" << (a/b);
       else if  (op=='%')
             cout  <<  "Remainder="  << (a%b);
       else
             cout  << "Invalid Input";
}



Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.