9/9/2012
For Next Time
Read Chapter 4 Expressions, and Arithmetic
Chapters 4
1 2
Arithmetic Operator Precedence
Just as in normal mathematics:
Operator Operation * and / are applied before + and – + Addition Parentheses can override the order of application - Subtraction * Multiplication / Division % Modulus
3
Operator Associativity Operator Arity
Is x + y + z evaluated as Unary: (x + y) + z -x
x + (y + z) +sum
Does it matter? Binary sum + diff
2 - ev
1 9/9/2012
Precedence and Associativity Mixed Arithmetic
In an expression involving different numeric Precedence from high to low types, the less dominant types are converted to the more dominant types for the purpose of Arity Operators Associativity evaluating the expression Unary +, - Example Binary *, /, % Left Binary +, - Left int x = 5; Binary = Right double y = 4.0, z; z = x + y;
Comments Source Code Formatting
Helpful to human readers Like comments: unimportant to compiler but Ignored by the compiler very important to human readers Single-line comments Some guidelines: // This is a brief note Each statement on its own line Block comments Align curly braces in a standard way /* This is a longer remark Indent bodies of functions and structured that covers statements several lines. */ Use spaces around binary operators
Errors Logic Errors
Compile-time errors Not detectable by the compiler
Violation of the rules of the C++ language Not detectable by the run-time environment Compiler tells you about them The program’s logic is not correct
Plentiful when you are first learning a language The hardest kinds of errors to diagnose and repair
Generally easy to fix The compile can provide no feedback
Runtime errors Can result from carelessness, lack of understanding of the problem, lack of understanding of the way the Depends on the situation of the running program: like dividing by a variable that has been assigned zero language works; for example: Wrote x - y, meant y - x The run-time environment terminates the program’s execution
2 9/9/2012
Logic Error? Additional Arithmetic Operators
Increment: ++ Decrement: -- Increase: += double degrees_F, degrees_C; Other operations similar to increase: -= cin >> degrees_F; *= degrees_C = 5/9*(degrees_F - 32); /= %=
Others . . .
Next . . .
Conditional execution
15
3