Operator Precedence - Any tips?

Feb 20, 2009 at 5:35am
Hey guys, just wondering if use know any tips in remembering the operator precedence. As I have to start knowing these off by heart and I'm always putting a couple in the wrong place hah. So yeah any tips on how to remember them all in order would be great :)

Cheers,
Myth.
Feb 20, 2009 at 1:07pm
some of them have the same precedence relations as in maths:
+ / higher precedence than + -
= += etc. have a really low precedence as they have to assign value from expressions
?: have a higher precedence than those as you should be able to write this : a = b ? c : d;
unary operators usually have a really high precedence eg: *ptr + 1

So remember how you would use them and you will remember them.
However if you are not sure you can use parentheses
Last edited on Feb 20, 2009 at 1:08pm
Feb 21, 2009 at 8:12pm
Thanks Bazzy what you've said I understand and know but - My problem is all of this has to be written in order on a written test. So I'm trying to pull my head around remembering them all in order correctly and not forgetting one.

Doing my best though to make sure I don't miss one :)
Feb 21, 2009 at 8:30pm
closed account (z05DSL3A)
Are you after remembering the precedence of all operators, or just maths type ones (not that I have any tips that I can think of)?
Feb 21, 2009 at 9:35pm
Bottem of this page so All of them

http://www.cplusplus.com/doc/tutorial/operators.html
Last edited on Feb 21, 2009 at 9:36pm
Feb 21, 2009 at 9:52pm
Here's a mnemonic I just made up:
So usually unless Steve, my dad, makes a saxophone, Steve can't exactly assemble xylophones or an oboe till a trashman comes.

Scope (::)
Unary special (function calls, casts, member access (like ->), POST ++/--, etc)
Unary normal (!, ~, - (as in -1), */&. new/delete, etc)
Selectors (->* and .*, I wouldn't worry about this one so much)
Multiplication/Divisision/Modulo
Addition/Subtraction
Shifts (<<, >>)
Comparators (non-equality, <, >, <=, >=)
Equality comparators (==, !=)
And - bitwise (&)
Xor - bitwise (^)
Or - bitwise (|)
And - logical (&&)
Or - logical (||)
Ternary ( cond ? resultIfTrue : resultIfFalse )
Assignment (=, +=, >>=, etc)
Throw (throw)
Comma (,)

Just remember that that math ones */% and +- go just like in normal math and not on separate levels like I've implied here, I just wanted to make it clear the general order.
Feb 21, 2009 at 10:18pm
Haha, thats pretty cool. Thanks a heap :)!
Topic archived. No new replies allowed.