CS 100, Summer 2001 Wednesday, 6/27 Lecture 3 ----------------------------------------------------------------------- + Collect Exercise 2 Announcements: (2 mins) + Assignment 1 due Thursday, 6/28 + reading: Savitch 3.2 ----------------------------------------------------------------------- Topics: + operators + expressions + statements + conditional statements ----------------------------------------------------------------------- Miscellaneous: - do check out the posted lecture notes - has examples used in class (.java and .txt links) - fixed-width fonts - who is reading the newsgroup? - incomplete surveys - partners: no (not yet) - CUid is CUid (not netid) - submit your Friday notes for fame and recognition ----------------------------------------------------------------------- + Operators: (Appendix 2 for full list) - Arithmetic: + - / * % (and more) + remainder: % + x % y generates the remainder of x/y + ex) int x1 = 10 % 5; // -> 0 (10/5 = 2 with 0 as remainder) int x2 = 10 % 3; // -> 1 (10/3 = 3 with 1 as remainder) - Comparison: < > <= >= == != - Boolean: && || ! - Ex) boolean b1 = 3 > 4; // false boolean b2 = 2.5 < 6.5; // true boolean b3 = 6 == 3; // false boolean b4 = !false; // true! - beware of mixing types! ex) System.out.println(1/2) yields 0, not 0.5 - mixing doubles and ints makes doubles ex) System.out.println(1.0/2) yields 0.5 ---------------------------------------------------------------------- + Expressions: - combine constants with operators - precedence: some operators come before others - associativity: operators "work" left to right or right to left ex) 1 + 1; 2 * 2 - 4; // technically means (2*2) - 4 because of precedence 2 - 1 - 3; // technically means (2-1) - 3 due to associativity ASK YOURSELF: Can you come up with a different result? - use () to force some expressions to evaluate first ex) (1 + 1); // () not needed 2 * (2 - 4); 2 - (1 - 3); ----------------------------------------------------------------------- Recall: characters -> tokens -> statements -> programs ---------------------------------------------------------------------- Statements: + combine tokens to create statements + end statements with semicolon + going to a new line does NOT mean you have a new statement! + Java executes each statement top-down, left-to-right (within a statement, operator precedence determines direction of execution) + Block statement: put {} around a group of statements (useful for "if") + Empty statement: doesn't actually do anything (useful for loops) Example: ; ---------------------------------------------------------------------- + Assignment: - changes the program's STATE - store value in a variable that's been declared - the value's type MUST match the variable's type - ex) int x; x = 1; ASK YOURSELF: What would be an invalid assignment to x? - think x <- 1 because assign operator associates right-to-left - can say in English "x gets 1" + You should be able to map out the program state as it progresses: Draw boxes to hold the values and update them with every statement. ------------------------------------------------------------------------------- + Swap example (see Swap.java) ------------------------------------------------------------------------------- + Style issues: - usually one statement per line - use whitespace to separate "paragraphs" of code - leave space between operators and operands (values operated upon) - use descriptive variable names - document your code (comments!) ----------------------------------------------------------------------- Conditional processing: + Motivation: sanity checks! 1. Check type of input (SavitchIn takes care of this for you) 2. Check valid values (this is your responsibility!) - Ex) give negative values to ChangeMaker + use if, if-else (and if-else if) + syntax: (You MUST use the { } for CS 100 purposes) if (c) // if c is true { s; // do s } // otherwise, skip this statement if (c) // if c is true { s1; // do s1 } else // otherwise { s; // do s2 } + other varieties: if (c1) { s1; } else if (c2) { s2; } if (c1) { s1; } else if (c2) { s2; } else { s3; } + can have as many else-ifs as you wish ------------------------------------------------------------- Brief QUIZ (see Quiz.java) ------------------------------------------------------------- PICK UP graded Exercise 1