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!
 

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

1. 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++.

2. 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.

3. 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.

4. 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.

5. 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

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top