Nested IF and ELSE in C ++

In this tutorial from our C++ course, we will learn how to use a very important programming technique, which is nested IF ELSE within IF ELSE.

IF and ELSE within IF and ELSE

Let's explain the technique of nesting IF and ELSE through examples.

Create a C++ program that asks for two numbers, and says if they are the same or one is bigger than the other (showing who's who).

Come on, let's store the numbers in the variables: num1 and num2
The first test is if (num1 == num2) to see if they are the same.

If so, it warns and the program is over.
If not, we have two options:

  1. num1 is greater than num2
  2. num2 is greater than num1

That is, we have to do one more test within ELSE.

Let's insert the test inside:
if (num1> num2)

If true, it says that num1 is greater than num2.

What if it's fake?
Then it must fall into another ELSE, this second nested IF, understand?

Let's see how our code looks:
#include <iostream>
using namespace std;

int main()
{
    float num1, num2;

    cout << "First number: ";
    cin >> num1;

    cout << "Second number:: ";
    cin >> num2;

    if(num1==num2)
        cout <<"Equal" << endl;
    else
        if(num1 > num2)
            cout << num1 <<" greater than "<<num2 << endl;
        else
            cout << num2 <<" greater than"<<num1 << endl;

    return 0;
}
Note that we made an indentation, that is, we gave a spacing so that each else is in the same vertical as its respective if.

Nested IF and ELSE Example

You have been hired by a hospital to check if people can donate blood or not.
They can only donate if they are 18 or older and have no disease.
Make a C++ program that asks how old a person is and if he or she has a disease, then tells whether they can donate blood or not.

Let's go first to the code:
#include <iostream>
using namespace std;

int main()
{
    int age, dis;

    cout <<"Your age: ";
    cin >> age;

    cout <<"Do you have any disease ?"<<endl;
    cout <<"1. Not" << endl;
    cout <<"2. Yes" << endl;
    cin >> dis;

    if( age>=18 ){
        if(dis==1)
            cout << "You can donate blood!";
        else
            cout << "You can't donate because is sick";
    }else{
        cout << "You must be 18 or older to donate blood";
    }

    return 0;
}
Well, let's go.
First we store the age in the age variable.
Then, the user must type 1 if he has no disease and 2 if he has, this answer will be stored in the dis variable.

Now the tests.
First, we check if you are 18 or older. If you do not have it, you will get ELSE and let them know you need to be 18 or older to donate.

If so, if you are 18 or older, we will check if it has no disease, ie if you entered 1.
If not, we advise that you can donate.
If he entered 2, he warns that sick people cannot donate blood.

Cool not?
Now a hacker test: run the program above and find some error.

IF and ELSE nested in C++

If you are under 16, you cannot vote. From 16 to 18 is optional. From 65 too.
From 18 to 65, voting is required. Make a C++ program that asks for the age of the user and tells them if they are required to vote, optional or not.

Well, let's go.
In the first IF we will soon put the 'guys' who are under 16, they can't vote.

If you are 16 or older, go to ELSE.
Within that ELSE, let's check if it's under 18 with an IF, if it says it is optional.

If it's not smaller, it's because it's 18 or older.
Let's test if you are under 65, if you are have to vote.

If you are no less, you fall in the last ELSE and the person is over 65, voting optionally.
See how our code looked:
#include <iostream>
using namespace std;

int main()
{
    int age;

    cout <<"Your age: ";
    cin >> age;

    if(age < 16)
        cout << "Can't vote, too young." << endl;
    else
        if(age < 18)
            cout << "Optional vote." << endl;
        else
            if(age < 65)
                cout << "Is required to vote." << endl;
            else
                cout << "Optional vote" << endl;

    return 0;
}

The IF/ELSE IF Command

Create a program that asks for a student's grade on a test, ranging from 0.0 to 10.
If the grade is greater than or equal to 9, say that he took A.
If it is 8.0 through 8.9, say that this person took B.
If it goes from 7.0 to 7.9, say she took C.
If it goes from 6.0 to 6.9, say she took D.
If it is below 6.0, she took F.

The first test to make is to see if the grade is greater than or equal to 9.0, if it is, the first IF ends the execution of the program.
If not, go to ELSE.

Inside ELSE we need to test with a new IF if the value is greater than or equal to 8.0.
You see, it only falls in this internal IF if it is from 8.0 to 8.9, above that it would have fallen in the first IF.

If it is not, it will fall into another internal ELSE.
Within this ELSE, let's test (IF) if it's from 7.0 to 7.9

If not, we create another ELSE and an IF within it to know if it's from 6.0 to 6.9

Finally, if none of the above, is grade F.

Let's see how our code looked:
#include <iostream>
using namespace std;

int main()
{
    float grade;

    cout << "Your grade ";
    cin >> grade;

    if(grade >= 9)
        cout << "A"<<endl;
    else
        if(grade >=8 )
            cout << "B"<<endl;
        else
            if(grade >=7 )
                cout << "C"<<endl;
            else
                if(grade >=6 )
                    cout << "D"<<endl;
                else
                    cout << "F"<<endl;

    return 0;
}
Note that C ++ code is 'going' to the right.
Imagine if you had 10 more conditions to test, what would our code look like?
A mess, isn't it?

Now if I take the same code and do this:
#include <iostream>
using namespace std;

int main()
{
    float grade;

    cout << "Your age ";
    cin >> grade;

    if(grade >= 9)
        cout << "A"<<endl;
    else if(grade >=8 )
        cout << "B"<<endl;
    else if(grade >=7 )
        cout << "C"<<endl;
    else if(grade >=6 )
        cout << "D"<<endl;
    else
        cout << "F"<<endl;

    return 0;
}
More readable, do you agree?
This is a good programming technique because it makes reading and understanding the code easier.

No comments:

Post a Comment