Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type arithmetic and type casting Shortcut operators Week 09 College of Information Science and Engineering Ritsumeikan University white spaces comment statements Quiz Recall Week 08 Compiler, interpreter Type, data type, ADT Declaration Variable Constant Literal Integer, float, char, boolean Identifier Assignment statement Our goals today To realize the importance of documenting the source code To understand how to correctly do arithmetic with computers, using high-level programming languages To understand arithmetic operator precedence White space To increase readability of your code, surround operators and operands with white space and skip lines between logical sections of program Space, tab, and new line are white space characters At least one white space character is required between a keyword (reserved word, operator) and identifier Any amount of white space characters are permitted between identifiers, keywords, operators, and literals Comment statements Comment statements (comments) explain the program to yourself and others There are two types of comments: Block comments Can span several lines Begin with /* and end with */ The compiler ignores everything from /* to */ Line comments Start with // The compiler ignores text from // to the end of line
Documenting programs Always include a block comment at the beginning of each source file (new program) identify the author of the program briefly describe the function of the program Expressions <left side> = <right side> Use line and block comments to explain the logic of the program (for example include pseudocode) Arithmetic expressions Arithmetic expressions compute numeric results and make use of the arithmetic operators: Operator + * / % Division and remainder If both operands to the division operator / are integers, the result is an integer (the fractional part is discarded), e.g. 14 / 3 8 / 12 addition subtraction division % usage examples Remainder operator is very useful in programming. For example: for any even number q, q % 2 is always 0, and for any odd number p, p % 2 is always 1 Another example: Suppose we know that January 1 is Saturday. We can then easily calculate that the day for February 1 is Tuesday: Saturday is the 6th day in a week (Sunday is 0th) January has 31 days A week has 7 days All this can be written as: (6+31)%7 that gives 2, the second day in a week which is Tuesday equals to equals to 4 0 The remainder modulus operator % (or multiplication modulus (remainder after division) An expression is a combination of one or more operators and operands that evaluate to a single value, such that: the value is assigned to left side left side is (must be) a single variable or named constant The value must be compatible with (convertible to) the left side s data type always explain constants used (there must be no unexplained magic coefficients ) explain algorithm by default assumptions always explain the program I/O (interface) Recall assignment statements: MOD ) returns the remainder after dividing the first operand by the second, e.g. 14 % 3 equals to 2 8 % 12 equals to 8 Taking modulus is an integer operation both operands must be integers Division by zero Integer division by 0: Example: int x = 4 / 0; This will result in no compiler error, but at run time, ArithmeticException message (fatal error) will be generated, and the program will stop executing Floating-point division by 0: If dividend is not 0, the result is Infinity If dividend and divisor are both 0, the result is NaN (not a number)
Precedence Operator precedence Operator Order of evaluation 1 ( ) left - right parenthesis for explicit grouping 2 * / % left - right multiplication, division, modulus 3 + - left - right addition, subtraction last = right - left assignment Example: x = a / b * (c + (d - e)) 5 3 4 2 1 Expression trees Sometimes, the evaluation of an expression is convenient to show using an expression tree The operators lower in the tree have higher precedence for that expression, e.g. a + (b c) / d a + b c / d Mixed-type arithmetic In most programming languages, when performing calculations with operands of different data types (on right side): Lower-precision operands are promoted to higher-precision data types, then the calculation is performed Promotion is effective only for expression evaluation and does not cause a permanent change to the data This is called "implicit type casting" Any expression involving a floating-point operand will have a floating-point result (on right side) Rules of promotion Applies the first of these rules that fits: 1. If either operand is a double, the other operand is converted to a double 2. If either operand is a float, the other operand is converted to a float 3. If either operand is a long, the other operand is converted to a long 4. If either operand is an int, the other operand is promoted to an int 5. If neither operand is a double, float, long, or an int, both operands are promoted to int Examples of promotion // Pseudocode: int x,y; // integers are 4 bytes using 2's compliment format short z; // shorts are 2 bytes using 2's compliment format float a; // floats are 4 bytes, but using floating point format x = 7; z = 3; a = 3.14; y = a * 2 + x/z; /* Explanation: a = 3.14 (floating point number) '2' is integer but is promoted to '2.0' a floating point number z is a short and will be promoted to an integer x/z involves only integers (7/3) so will give a result of 2 (an integer) then the integer 2 will be promoted to 2.0 (a floating point number) */ calculation: 3.14 * 2.0 + 2.0 = 8.28 (a floating point number) finally y is assigned an integer 8 since y is an integer data type (the fractional part is removed) Explicit type casting Syntax: (datatype)( expression ) Examples: x = 2.5 + (float)5/(float)2; c = (int)(x - 3); Note that parentheses around expression are optional if expression consists of one variable Useful for calculating averages (think, why?)
Shortcut operators ++ is increment by 1, and -- is decrement by 1. Examples: count++; // count = count + 1; count--; // count = count - 1; Postfix version (var++, var--): use value of var in expression, then increment or decrement Prefix version (++var, --var): increment or decrement var, then use its value in expression Examples of shortcuts // Pseudocode: int x,y,z; z = x++ + y; // first z is 7; then x = 3 z = ++x + y; // first x = 3; then z is 8 z = x++ + ++y + y/2; /* first y = 6, then it becomes x++ + 6 + 6/2 z = 2 + 6 + 3 z = 11 then x = 3 */ Other shortcut operators Operator Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10; Note that no spaces are allowed between the arithmetic operator and the equals sign Precedence Operator Operator precedence Order of evaluation 1 ( ) left - right parenthesis for explicit grouping 2 ++ -- right - left preincrement, predecrement 3 * / % left - right multiplication, division, modulus 4 + - left - right addition or String concatenation, subtraction 5 = += -= *= /= %= right - left assignment After evalua tion ++ -- right - left postincrement, postdecrement Concepts you need to know after this class White spaces and comment statements: Why we do need to document programs Arithmetic expressions: Operators and operator precedence (including shortcuts) Mixed-type arithmetic rules and type casting Homework Read these slides again Do the self-preparation assignments Learn the vocabulary Consult, whenever necessary, the textbook materials
Next class Boolean expressions Binary logic Relational and logical operators Operator precedence Boolean and algebraic expressions Write your name (in English), student ID number, and solve the following problems (write only answers): 1. Quiz 04