CS100J, Spring 2001 Tues 2/5 Lecture 5 ------------------------------------------------------------------------------- Announcements: + P2 due Thurs 2/8 (include E2 even if you did it!) + T1 (prelim 1) Tues 2/13 + AEWs for CS100J: some sopts open (see Announcements) + new section: Tues (today) 12:20-1:10 Philips 407 + 3.3 in Savitch: use $while$ + give lecture notes and online examples "speech" ------------------------------------------------------------------------------- Topics: + notation: $...$, boldface-courier + operators: cast, %, ++, --, += + loops: $while$, patterns, types + methods: $Math.random$, $Math.floor$, $Math.pow$ (see pp316-318) + scope diagrams? (might have to hold off on this) ------------------------------------------------------------------------------- Summary from Lecture 4: + loops: repeat actions + known as REPETITION statements + $while(c)$ s; - c must be boolean - s might be any statement + BLOCK (COMPOUND) statement {s1; s2; ..., sn;} - all statements grouped together - typical use for condition and selection statements + loop design: - preprocessing (gather, assign data) - processing (enter loop by testing condition, do tasks) - postprocessing (account for what may have happened) ------------------------------------------------------------------------------- Important Examples: + See Spring 2000 examples for additional help + everything up to nested2 ------------------------------------------------------------------------------- I/O: + in labs, CUCS (TokenReader) already installed + just include statement: TokenReader in = new TokenReader(System.in); + that line will show up on exams + for those w/o TR, put TR or equivalent in Project (.java in Sources, .class in Classes) ------------------------------------------------------------------------------- More on operators: + the followings operators show up in loops often + remainder: % (pp70-71) - x % y generates the remainder of x/y - ex) 10 % 5 -> 0 (10/5 = 2 with 0 as remainder) 10 % 3 -> 1 (10/3 = 3 with 1 as remainder) + increment: ++, --, += (pp77-79) - used for shortcuts ex) $count = count + 1$ replaced with $count++$ or $++count$ - works also with -- to decrease by 1 - are ++var and var++ identical? NO ex) int a,b,c,d; c = 1; d=1; a = ++c; // a gets c<-c+1: so, c gets 2, and then a gets 2 b = d++; // b gets c and then c<-c+1: so, b gets 1 and then d gets 2 - others: $count = count + 7$; $count += 7$; ------------------------------------------------------------------------------- More on assignments: + swapping variable values + shows up in loops often int tmp, x, y; x=1;y=2; tmp = y; y = x; x = y; ------------------------------------------------------------------------------- 2 generalizations of loops: 1) definite - repeat/echo (could use $for$ -- discussed later) - accumulate indefinite - conditional update 2) patterns ------------------------------------------------------------------------------- Style: + indentation (under $while$) + avoid $break$, $System.exit$ (pp 175-177) -------------------------------------------------------------------------------