CS100J, Fall 2001 Tues 9/28 Lecture 6 ------------------------------------------------------------------------------- Announcements: + Mike Scovetta filling in today + AEW reps visiting + 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 ------------------------------------------------------------------------------- Topics: + selection statements + $while$ + categories of loops + increment operators ------------------------------------------------------------------------------- Repetion statements/loops: + what if you need to repeat something? - you could use lots of assignments and conditions - you want to avoid redundancy to clarify code + use REPETITION STATEMENTS to repeat actions + introducing new keyword: $while$ + syntax for $while$: while(c) s; c: boolean expression s: might be any statement or block statement reminder: block statement {s1; s2; ..., sn;} - all statements grouped together - typical use for condition and selection statements ------------------------------------------------------------------------------- example: toy probs ------------------------------------------------------------------------------- Other keywords: + $for$ + $do$ + not allowing these for awhile (forgive the pun) + why? $while$ is "most pure" ------------------------------------------------------------------------------- Style: + indentation (under $while$) + avoid $break$, $System.exit$ (pp 175-177) ------------------------------------------------------------------------------- Increment operators: ++, --, +=, *= (pp77-79) + used for shortcuts ex) $count = count + 1$ replaced with $count++$ or $++count$ + works also with -- to decrease by 1 + Question: ++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$; ------------------------------------------------------------------------------- Loop design: + preprocessing (gather, assign data) + processing (enter loop by testing condition, do tasks) + postprocessing (account for what may have happened) + 2 generalizations of loops: (1) definite - repeat/echo (could use $for$ -- discussed later) - accumulate (2) indefinite - conditional update -------------------------------------------------------------------------------