Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Order of Operations

When more than one operator appears in an expression the order of evaluation depends on the rules of precedence. A complete explanation of precedence can get complicated, but just to get you started:

  1. Multiplication and division happen before addition and subtraction. So 2*3-1 yields 5 , not 4 , and 2/3-1 yields -1 , not 1 (remember that in integer division 2/3 is 0 ).

  2. If the operators have the same precedence they are evaluated from left to right. So in the expression iMinute*100/60 , the multiplication happens first, yielding 5900/60 , which in turn yields 98 . If the operations had gone from right to left, the result would be 59*1 which is 59 , which is wrong.

  3. Any time you want to override the rules of precedence (or you are not sure what they are) you can use parentheses. Expressions in parentheses are evaluated first, so 2 * (3-1) is 4 . You can also use parentheses to make an expression easier to read, as in (iMinute * 100) / 60 , even though it doesn’t change the result.

Note: Incorrect understanding (usually accidental) of operator precedence is another major cause of logic errors in a program. If there is a chance of misunderstanding, it is a good idea to use parentheses.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll