What is the Precedence of Mathematical Operators in C++?

  • C/C++
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    C++
In summary, the operators * and / have equal precedence and are evaluated from left to right. They have higher priority than + and -, which are also evaluated from left to right. This follows the generally accepted order of operations in real life.
  • #1
needOfHelpCMath
72
0
Which has precedence over each other * / - + because i am working on this problem for c++ but without coding it but instead writing it out on paper. For example:
HTML:
7+3*4/2-5-3+4

Which will have precedence - or /?
 
Technology news on Phys.org
  • #2
needOfHelpCMath said:
Which has precedence over each other * / - + because i am working on this problem for c++ but without coding it but instead writing it out on paper. For example:
HTML:
7+3*4/2-5-3+4

Which will have precedence - or /?

Hey needOfHelpCMath! (Wave)

The operators [M]*[/M] and [M]/[/M] have the same priority and are evaluated from left to right.
They have higher priority than [M]+[/M] and [M]-[/M], so are evaluated first.
After that the operators [M]+[/M] and [M]-[/M] have equal priority, and are also evaluated left to right.

This is exactly the same as the generally accepted order in 'real life'.

It means that the expression is evaluated as:
Code:
7+3*4/2-5-3+4 = 7+((3*4)/2)-5-3+4 = 7+6-5-3+4 = ((((7+6)-5)-3)+4)
 
  • #3
Thank you very much!
 

FAQ: What is the Precedence of Mathematical Operators in C++?

What is Dijkstra Precedence in C++?

Dijkstra Precedence is a method for resolving conflicts in expressions with multiple operations of the same precedence level. It was developed by computer scientist Edsger Dijkstra and is commonly used in programming languages like C++.

How does Dijkstra Precedence work in C++?

Dijkstra Precedence works by assigning different levels of priority to different operators. When evaluating an expression, the operators with higher priority are evaluated first. This helps to avoid ambiguity and ensures that the expression is evaluated in a predictable manner.

What are the benefits of using Dijkstra Precedence in C++?

Using Dijkstra Precedence in C++ helps to avoid errors and ensures that expressions are evaluated in a consistent and predictable manner. It also allows for more complex expressions to be written without the need for excessive use of parentheses.

Are there any drawbacks to using Dijkstra Precedence in C++?

One potential drawback of using Dijkstra Precedence is that it may not be intuitive for all programmers, as it differs from the traditional order of operations taught in mathematics. It may also require additional parentheses in some cases to ensure the correct order of evaluation.

Can Dijkstra Precedence be overridden in C++?

Yes, Dijkstra Precedence can be overridden in C++ by using parentheses to group operations in a different order. This allows for more flexibility in writing complex expressions, but it is generally recommended to follow the standard Dijkstra Precedence rules for consistency and readability.

Similar threads

Replies
23
Views
2K
Replies
5
Views
2K
Replies
10
Views
2K
Replies
2
Views
2K
Replies
25
Views
2K
Back
Top