The IF conditional statement in C++

In this C ++ tutorial, we will learn how to use the IF conditional statement.
This will make our programs more flexible and dynamic.

Making decisions in C++

So far, all our codes have been purely sequential.
That is, they ran from top to bottom in the main function, executing line by line, command by command, and always like this.

If we run our programs 1 million times, they do the same 1 million times.
But is this what happens in everyday programs?

When you click one thing, you open one thing, if you click another, something else opens.
If you type a command, something happens. If you type another, something else happens.

That is, what your computer will do depends on what you do, it needs to do some testing before making what decision it should make.

Let's learn how to make these decisions now using the if conditional statement.

The IF command in C++

The structure of the IF command is very simple and easy to understand, see:
if ( conditional_statement ){
  // code
  // code
  // code
}
We start with the if statement, then open parentheses, and inside it must have some boolean value (1 or 0, true or false), in which case we usually do some relational operation (comparison test), then some code in braces .

The operation is quite simple: the if statement will analyze what's inside the parentheses.

If what is there is true, it executes the code in braces.
If what's inside is false, it does nothing, if is skipped, as if it didn't exist.

So read: if true, then do it.

How to use IF in C++

The program below has a conditional statement:
2 > 1

If this test is true, cout inside the IF is performed:
#include <iostream>
using namespace std;

int main()
{
    if( 2 > 1){
        cout << "Well, 2 is greather than 1";
    }
    return 0;
}
Obviously true, so the message always appears on the screen.
Now test with 2 <1

What happened?

Usage example of IF

Make a program that asks the user for their age and let them know if they are older.

A person is older if they are 18 or older, so our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int age;

    cout << "What's your age: ";
    cin >> age;

    if( age >= 21){
        cout << "You're adulthood";
    }

    return 0;
}
Can you solve this exercise using the > operator instead of >=?

IF Usage Example

Ask the user for a grade, and if it is less than 6, say that he failed the test.

See how the code looks:
#include <iostream>
using namespace std;

int main()
{
    float grade;

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

    if( grade < 6)
        cout << "Failed";

    return 0;
}
Note that we do not use braces, this is because the IF code is composed of only one instruction line. In such cases, you do not need to enclose it in braces.

But if the statement within the IF is larger, use braces.

See how C ++ is a strict teacher, I put the grade 5.99999 and yet he disapproved:
IF and ELSE in C++ tutorial


Using IF in C++

Make a program that asks the user for two numbers, and tell them if they are the same.
#include <iostream>
using namespace std;

int main()
{
    float num1, num2;

    cout << "Number 1: ";
    cin >> num1;

    cout << "Number 2: ";
    cin >> num2;

    if( num1 == num2)
        cout << "Equal!";

    return 0;
}
Pretty simple, isn't it?

Note that if you enter different numbers, IF receives the false result on the conditional statement and does not execute your code.

That is, nothing happens, the program simply skips the IF.

Exercise

Write a program that prompts the user for a numeric password and displays an error message if he ERRORS the password. The password is 2112.

Post your solution in the comments.

No comments:

Post a Comment