1/28/2011 For Next Time Read Chapter 4 Expressions and Arithmetic Chapters 4 1 2 CPTR 124 Checklist Type Conversions Read the appropriate chapter(s) Re-read the appropriate chapter(s) int double, is this OK Download the code we develop in class double int, is this OK? Experiment with the class code Start working on the next lab Visit the tutors if necessary Check WebGrades periodically 4 Data Ranges Arithmetic Operator Operation + Addition - Subtraction * Multiplication / Division % Modulus 7 1
1/28/2011 Operator Precedence Operator Associativity Is x + y + z evaluated as Just as in normal mathematics: * and / are applied before + and – (x + y) + z Parentheses can override the order of application x + (y + z) Does it matter? Operator Arity Precedence and Associativity Unary: Precedence from high to low -x +sum Arity Operators Associativity Binary +, - Unary sum + diff Binary *, /, % Left 2 - ev +, - Binary Left Binary = Right Mixed Arithmetic Comments In an expression involving different numeric Helpful to human readers types, the less dominant types are converted Ignored by the compiler to the more dominant types for the purpose of Single-line comments evaluating the expression // This is a brief note Example Block comments int x = 5; /* This is a longer remark double y = 4.0, z; that covers z = x + y; several lines. */ 2
1/28/2011 Source Code Formatting Errors Like comments: unimportant to compiler but Compile-time errors very important to human readers Violation of the rules of the C# language Compiler tells you about them Some guidelines: Plentiful when you are first learning a language Each statement on its own line Generally easy to fix Align curly braces in a standard way Runtime errors Indent bodies of methods and structured Depends on the situation of the running program: like statements dividing by a variable that has been assigned zero Use spaces around binary operators The run- time environment terminates the program’s execution Logic Errors Warnings Not detectable by the compiler Not a violation of the C# language Not detectable by the run-time environment Indicates that the programmer may be The program’s logic is not correct making a mistake The hardest kinds of errors to diagnose and repair Indicates a potential logic error The compile can provide no feedback Do not prevent the compiler from producing Can result from carelessness, lack of understanding of the problem, lack of understanding of the way the executable code language works; for example: Example: Wrote x - y , meant y - x Declaring a variable but not using it Logic Error? Additional Arithmetic Operators Increment: ++ Decrement: -- Increase: += Other operations similar to increase: double degrees_F, degrees_C; -= degrees_F = double.Parse(Console.ReadLine()); *= degrees_C = 5/9*(degrees_F - 32); /= %= Others . . . 3
1/28/2011 Next . . . Classes and objects 20 4
Recommend
More recommend