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

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
In C++, the order of operations dictates that multiplication (*) and division (/) have the same precedence and are evaluated from left to right, taking priority over addition (+) and subtraction (-), which also share the same precedence and are evaluated left to right. For the expression 7+3*4/2-5-3+4, the evaluation proceeds as follows: first, the multiplication and division are calculated, resulting in 7+6-5-3+4, which is then simplified step by step to arrive at the final result. This follows the standard mathematical conventions for operator precedence.
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!
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
Back
Top