CS253: Software Development with C++

Spring 2018

Order Of Evaluation

See this page as a slide show

CS253 Order Of Evaluation

Basic Expressions

Consider the expression a+b, which parses as:

        			 ┌───┐
        			 │ + │
        			 └───┘
        			 ⠌   ⠡
        			⠌     ⠡
        		    ┌───┐     ┌───┐
        		    │ a │     │ b │
        		    └───┘     └───┘

Basic Expressions

How does a+b*c parse?

                  ┌───┐                       ┌───┐
                  │ * │                       │ + │
                  └───┘                       └───┘
                  ⠌   ⠡                       ⠌   ⠡
                 ⠌     ⠡                     ⠌     ⠡
             ┌───┐     ┌───┐             ┌───┐     ┌───┐
             │ + │     │ c │             │ a │     │ * │
             └───┘     └───┘             └───┘     └───┘
             ⠌   ⠡                                 ⠌   ⠡
            ⠌     ⠡                               ⠌     ⠡
        ┌───┐     ┌───┐                       ┌───┐     ┌───┐
        │ a │     │ b │                       │ b │     │ c │
        └───┘     └───┘                       └───┘     └───┘

The right-hand side, of course. * has higher precedence than +.

Basic Expressions

Let’s use parentheses. How does (a+b)*c parse?

                  ┌───┐                       ┌───┐
                  │ * │                       │ + │
                  └───┘                       └───┘
                  ⠌   ⠡                       ⠌   ⠡
                 ⠌     ⠡                     ⠌     ⠡
             ┌───┐     ┌───┐             ┌───┐     ┌───┐
             │ + │     │ c │             │ a │     │ * │
             └───┘     └───┘             └───┘     └───┘
             ⠌   ⠡                                 ⠌   ⠡
            ⠌     ⠡                               ⠌     ⠡
        ┌───┐     ┌───┐                       ┌───┐     ┌───┐
        │ a │     │ b │                       │ b │     │ c │
        └───┘     └───┘                       └───┘     └───┘

The left-hand side, of course. Parentheses mean “do this first”.

No

Really

              ┌───┐
              │ * │
              └───┘
              ⠌   ⠡
             ⠌     ⠡
         ┌───┐     ┌───┐
         │ + │     │ c │
         └───┘     └───┘
         ⠌   ⠡
        ⠌     ⠡
    ┌───┐     ┌───┐
    │ a │     │ b │
    └───┘     └───┘

Really

              ┌───┐
              │ * │
              └───┘
              ⠌   ⠡
             ⠌     ⠡
         ┌───┐     ┌───┐
         │ + │     │ c │
         └───┘     └───┘
         ⠌   ⠡
        ⠌     ⠡
    ┌───┐     ┌───┐
    │ a │     │ b │
    └───┘     └───┘

Possible orders of evaluation:

  1. a, b, c, +, *
  2. a, b, +, c, *
  3. a, c, b, +, *
  4. b, a, c, +, *
  5. b, a, +, c, *
  6. b, c, a, +, *
  7. c, a, b, +, *
  8. c, b, a, +, *

Who Cares?

Some Operators Are Different

A user-defined operation (e.g., operator+) does not have order determined, because it’s a function called with two arguments. The order of evaluation of function arguments is unspecified.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:55

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building