CS 100, Summer 2001 Thursday, 6/28 Lecture 4 ----------------------------------------------------------------------- + Collect Assignment 1 Announcements: + Pick up (graded) Exercise 2 + Exercise 3 due Friday + reading: Savitch 2.2 (Strings) and 3.1 (Cond operator) + Ben and Hongie's office location (328) + Assignment 2 will be handed out on Monday (7/2), not Friday ----------------------------------------------------------------------- Topics: + more operators + conditional statements + while loops ----------------------------------------------------------------------- Misc: + taking notes: don't need to write all the code down (see webpage) + do write down what goes on the board ----------------------------------------------------------------------- Review/summary from last time + operators (arithmetic, comparison, boolean) + expressions + statements (empty, block, assignment) + conditional statements (if, switch) ----------------------------------------------------------------------- + Operators: + type casting. (see Lecture4.java) - ex) mixing doubles and ints makes doubles System.out.println(1.0/2) yields 0.5 - use CAST to force different value - syntax: (type) expression - ex) System.out.println( (int) 9.8) yields 9 (an integer) - You also saw this in Exercise 1 + operators: ++, --, +=, etc. ---------------------------------------------------------------------- Statements: + Assignment: - changes the program's STATE + Java executes each statement top-down, left-to-right (within a statement, operator precedence determines direction of execution) + some statements can change FLOW OF CONTROL (order of which statements execute) ----------------------------------------------------------------------- Conditions: + Motivation: 1) make choices, 2) sanity checks + language elements: - if, else are keywords - conditions? must evaluate to true or false (Boolean) o relations: < (less than) > (greater than) <= (less than or equal) > (greater than or equal) == (equal, but do NOT use =) != (not equal) o logic: && (and) || (or) ! (not) o values: true false - example: see Lecture4.java + nested selection statements + each selection statement is indeed a statement + so, the statements in the selection statements could be another selection! + ex) if (temperature < 100) if (temperature > 80) System.out.println("hot"); else System.out.println("not hot"); + else AMBIGUITY (dangling else: which 'if' does the 'else' go to?) + Java default: 'else' attaches to nearest 'if' + indentation doesn't solve this + hence our rule: ALWAYS use curly braces! ----------------------------------------------------------------------- Loops: + statement for repeated actions - repeat calculation - accumulate values (statistics) - clues that you should use a loop: "do something to every..", "do something until..." + powerful addition to your Java repertoire! + loops are known as REPETITION statements + while (b) { s; } - b must be boolean - s might be any statement EXAMPLE: Div3.java, WhileLoop.java + loop design: - setup (gather data, assign variables) - condition (check for when to stop) - processing (do tasks, update) - postprocessing (account for what may have happened; output) + more examples next time! ----------------------------------------------------------------------- PICK UP Exercise 3 (handout), Exercise 2 (graded)