CS100J, Fall 2001 Thurs 9/20 Lecture 7 ------------------------------------------------------------------------------- Announcements: + Thanks Mike! + new? responsible for www.cs.cornell.edu/Courses/cs100j/2001fa/ start with Overview and Grading-->Adding + reading next week: Chap 4.1, Chap 5.1-5.4 (9/25,9/27) + Next-Next week: All of Chap 4 + TokenReader Mystery? see Java-->CUCS Files + section: 3rd attempt.... ------------------------------------------------------------------------------- Topics: + reminders from Tuesday + $while$ + categories of loops + increment operators ------------------------------------------------------------------------------- Reminders: + repetition statements - could do millions of $if$s, but why would you want that? - use $while$ + syntax while(c) { s1; s2; } + variables in { } - { } indicates block statement - can declare var in block if not declared BEFORE the block - cannot redeclare *within the same* block, but repetition statement allowed to use the same block + increment operators: ++, --, +=, *= (pp77-79) - used for shortcuts - ++, --, +=, *= a++: postincrement ++a: preincrement + not allowing $for$, $do$-$while$ for "awhile" (forgive the pun) + $break$, $System.exit$ - $break$: breaks out of loop $exit$: exits from program - avoid! generally interrupts control flow, which makes it hard to trace + other issues: - sentinel values: values to stop loop - named constants: initialize before loop to generalize code + generalizations of loops: (1) definite - repeat/echo (could use $for$ -- discussed later) - accumulate (2) indefinite - conditional update + built-in/imported methods (so far) - main - System.out.println() - equals (equalsIgnoreCase, and so forth) - SavitchIn.readInt (and so forth) - Math.sqrt, Math.pow, Math.max, Math.min (pp 316-318) Math.random (random.txt: online notes) ------------------------------------------------------------------------------- How to Design Loops - preprocessing - looping - postprocessing -------------------------------------------------------------------------------