How we can use nested if and else statement in c++ code


Nested if and else statement is very easy. In this case we use if key word with the first  statement and else key word of last statement and and between first and last statement we use else if key word with all the statement in c++. You can watch the code to understand in right way.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
    int num;
    cout<<"Enter a number : ";
    cin>>num;
    if(num>10&&num<30)
    {
                cout<<"How are you. ";
                }
                else if(num>30&&num<50)
                {
                                cout<<"Hello";
                }
                else if(num>50&&num<70)
                {
                                cout<<"Hay";
                }
                else
                {
                                cout<<"You should fine. ";
                }
}

Output:


When we give input  9 than the output will be

Enter a number : 9
You should fine.

When we give input  15 than the output will be

Enter a number : 15
How are you.

When we give input  40 than the output will be

Enter a number : 40
Hello

When we give input  66 than the output will be

Enter a number : 66
Hay

avatar
AUTHOR: Muhammad Abbas
I am Software Engineer.

0 Comments