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

  • Context: C/C++ 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The precedence of mathematical operators in C++ dictates that multiplication (*) and division (/) have higher priority than addition (+) and subtraction (-). Both * and / are evaluated from left to right, followed by + and - which also share equal precedence and are evaluated left to right. For the expression 7+3*4/2-5-3+4, the correct evaluation order leads to the result of 9. This follows the standard mathematical conventions recognized in programming.

PREREQUISITES
  • Understanding of C++ programming language syntax
  • Familiarity with mathematical operator precedence
  • Basic knowledge of arithmetic operations
  • Ability to evaluate expressions step-by-step
NEXT STEPS
  • Study C++ operator precedence rules in detail
  • Practice evaluating complex expressions in C++
  • Learn about operator overloading in C++
  • Explore debugging techniques for expression evaluation in C++
USEFUL FOR

C++ programmers, computer science students, and anyone looking to deepen their understanding of mathematical operations and expressions in programming.

needOfHelpCMath
Messages
70
Reaction score
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
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)
 
Thank you very much!
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
69
Views
10K
  • · Replies 17 ·
Replies
17
Views
3K