90 likes | 243 Views
Chapter 7. Expression Assignment Statements. Expressions. Syntax & Semantics of the expression. Arithmetic Expressions: Consist of operators , operands , parentheses , and function calls . Operators : Unary : meaning they have single operand . Ex: i ++
E N D
Chapter 7 Expression Assignment Statements
Expressions • Syntax & Semantics of the expression. • Arithmetic Expressions: • Consist of operators, operands, parentheses, and function calls. • Operators: • Unary: meaning they have single operand. • Ex: i++ • Binary: meaning they have two operands.
Expressions • Operator Evaluation Order: • Precedence • Associativity • Parentheses • Conditional Expressions
Expressions • Precedence: • *, / • +,- Consider the following expression: Where: a =5 b= 10 c=2. a + b * c Solution: =5+10*2 =5+20 =25
Expressions • Associativity: Consider the following expression: Where: a =5 b= 10 c=2 d=3 a + b - c – d Solution: =5+10-2-3 =15-2-3 =13-3 =10 a + c * d – b =5+2*3-10 =5+6-10 =11-10 =1
Expressions • Parentheses: Consider the following expression: Where: a =5 b= 10 c=2 d=3 a + c * d – b (a + c) * (d – b) (5+3)*(3-10) (8)*(-7) -54
Expressions • Conditional Statements: If ( count == 10 ) cout << “ Hello “;
Assignment Statements • Simple assignment: <target Variable> <assignment operator> <expression> Area = tall * width 2. Compound assignment: a = a + b is equivalent to a+=b
Assignment Statements 3. Unary assignment: Sum = count