Expressions, and how to compute their value

Definitions.

  1. places.C: three places where we can write an expression in a C++ program.
  2. An expression could be built out of literals
    	int i {10 + 20}; //i is born holding 30
    
    or out of variables that are currently holding a value:
    	int j {10};      //j is born holding 10
    	int k {j + 20};  //k is born holding 30
    
    Don’t do this:
    	int j;           //j is born containing unpredictable garbage
    	int k {j + 20};  //k is born containing bigger unpredictable garbage
    
    And the following snippet will be totally rejected by our C++ compiler c++. The program won’t even “compile”.
    	int k {j + 20};  //The computer doesn't know yet what "j" means.
    	int j {10};
    
  3. precedence.C:
    * (multiplication, line 5) has higher precedence then + (addition, line 6).
    - (negative, line 3) has higher precedence then - (subtraction, line 6).
    See lines 5 and 6 of this table.
    Note that - (negative, line 3) is a unary operator, while - (minus, line 6) is a binary operator. That’s how you tell them apart.
  4. associativity.C: addition and subtraction have left-to-right associativity.
    See line 6 of this table (the rightmost column).
  5. lobster.C. Example of an expression.
     

    Then you bank the heat and let the kettle simmer—ten minutes for the first pound of lobster, then three minutes for each pound after that. (This is assuming you’ve got hard shell lobsters, which, again, if you don’t live between Boston and Halifax is probably what you’ve got. For shedders [soft-shell], you’re supposed to subtract three minutes from the total.)

     
    David Foster Wallace, Consider the Lobster