The IF and ELSE Statement in C++

In this tutorial of our C ++ Online Course, we will teach you how to use the IF ELSE statement, without doubt one of the most important programming commands.

IF and ELSE Statement from C++

In the last tutorial, we learned how to use the IF conditional statement test, and saw that it does a test (huh, swear?), And if the result is TRUE, it executes a specific block of code. And if it is FALSE, it does not execute, it simply skips the IF.

Well, there's a problem there ... if it's TRUE, do something ... would be interesting if you did something else if it was FALSE, would you agree? This is where the ELSE command comes in.

The IF ELSE duo has the following syntax:
if (test){
   // Code in case
   // the test be TRUE
}
else {
   // Code in case
   // the test be FALSE
}
See I said duo. ELSE only exists if it has an IF.
IF may come alone, as we saw in the previous tutorial, but ELSE only comes with IF and always after it, as shown in the scope of this command.

Let's practice?

IF and ELSE Example

You were hired to make a program for a concert hall. In a certain piece of code, you will ask the age of the customer. If you are of legal age (21 years or older), please let him know that he can buy the ticket. If the customer have less, your program should state that he cannot enter the house.

First, we ask and store the age of the user in the integer variable age.
Now let's test if it is greater than or equal to 21.

If so, it falls in IF and it is larger.
If not, he drops to ELSE and we warn him that he cannot enter the place.

See how our code looked:
#include <iostream>
using namespace std;

int main()
{
    int age;

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

    if (age>=21){
        cout << "Ok, you can buy tickets!" << endl;
    } 
    else {
        cout << "You're underage, can't come in!" << endl;
    }

    return 0;
}
Can you rewrite the above program using the relational operator > instead of >=?

How to use IF and ELSE

Your teacher hired you to create a program that gets the student's grade. If it is 7.0 or higher, it has been approved. Otherwise, it is failed.

Here, no mystery, isn't it?
Only make conditional test greater than or equal to 7:
#include <iostream>
using namespace std;

int main()
{
    float grade;

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

    if (grade>=7)
        cout << "You are approved." << endl;
    else
        cout << "Damn, you failed." << endl;

    return 0;
}
See that we do not use the braces. In this case it is ok, because after the IF command and after the ELSE, there is only a single code statement.

If inside your IF or ELSE you have more than one line of code to run, enclose everything in braces, ok?

Using IF and ELSE in C ++

Write a program that asks a person's salary. If it is greater than $ 3,000, you have to pay 20% tax. If it is smaller, it has to pay 15%. Display the amount of tax the person has to pay.

We will store the salary in the sal variable and the tax the value of the tax.
Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    float sal, tax;

    cout << "Your paycheck ";
    cin >> sal;

    if (sal > 3000){
        tax = 0.20;
        cout << "Tax to pay: $ " << tax*sal <<endl;
    }
    else{
        tax = 0.15;
        cout << "Tax to pay: $ " << tax*sal <<endl;
    }
    return 0;
}
First, we test if the salary is greater than 3,000.
If so, the rate will be 20%. Otherwise it will be 15%.

Then we just calculate the amount of tax due.
Very simple, no?

Can you shorten the previous code? Show in the comments.

No comments:

Post a Comment