Operator Precedence and Grouping Expression with Parentheses

Now that you have learned everything from the last tutorial on mathematical operations in C++, answer, head on, for us how much the following expressions are worth:
  • 1 + 6*3
  • (1+6)*3
  • 1+6/2
  • (1+6/2)

Order of Mathematical Operators

Let's take the first and third expressions.
In the first one, we have two solutions you can find:
  • 1 + 6*3 = 1 + 18 = 19
Or:
  • 1 + 6*3 = 7 * 3 = 21
In the first calculation, we did the multiplication first and then the sum.
In the second calculation, we first summed 1 to 6, and then multiplied by 3.
Which one is right?

Already the third expression, can be resolved like this:
  • 1+6/2 = 1 + 3 = 4
Some might calculate like this:
  • 1+6/2 = 7 / 2 = 3.5
In the first case, we did the division operation first. In the second case, first we add 1 to 6, only then do the division.

Let's put both expressions in a program, so C ++ will tell us which one is correct.
But first, think about which one you think is right.

Then make a program to calculate both expressions and see the result.
Our code looks like this:

#include <iostream>
using namespace std;

int main()
{
    cout << "1 + 6*3 =  "<< 1 + 6*3 <<endl;
    cout << "1+6/2 =  "<< 1+6/2<<endl;

    return 0;
}

And the result:
Curso de C++ online grátis com apostila para download

Precedence of Mathematical Operators in C++

Imagine if NASA simulates a rocket launch and a calculation results in the value 21.
But then, during the official release, another machine does the same calculation and results in a value of 12?
Damn!

Therefore, C ++ has established an order to do the calculations, a precedence between operators.
As we can see, the multiplication and division calculation was first performed before the addition.

Order of operators is as follows:
  1. *  /  %
  2. + -
That is, C++ saw a mathematical expression, the first thing you will see is whether it has a division, multiplication or module operator. If you have one, solve it first.

If you have more than one of these, resolve from left to right.

Only then will you check for any addition or subtraction.
If you have more than one? It's going from left to right too, ok?

Parentheses () - Organization and Precedence

If we enclose an operation in parentheses, it will always be performed first.
It is as if the () had a higher precedence than the mathematical operators.

For example, on account: 1 + 6 * 3
If we want the sum to be done first, we do: (1 + 6) * 3
If we want to make sure that the multiplication is done first: 1 + (6 * 3)

Suppose we want to calculate the arithmetic mean of 5 numbers, in mathematics this is given by:

How calculate average in C++

A person who has not well learned operator precedence can do:
m = a + b + c + d + e / 5;


The mistake is that only the variable e will be divided by 5, can you understand that?
The correct thing is to use parentheses:
m = (a + b + c + d + e) / 5;

So, besides getting the bill right, it gets more organized too.
Do you agree?

C ++ Exercises

1. The equation of a line is given by: y = mx + c
Write this equation in C ++ using the operators correctly.

2. Write the following algebraic equations in the correct form in C ++:

Free and complete C++ tutorial for download PDF

3. An equation of the second degree is given by: ax² + bx + c = 0
Write it in C ++ language.

4. Still in the previous equation, how would we calculate the delta?

5. And the roots of the equation of the second degree?

Study References

Complete list of precedence of other operators as well:
https://en.cppreference.com/w/cpp/language/operator_precedence

No comments:

Post a Comment