BOOK NOTES CHAPTER 7 7.1 WHILE LOOPS - repeatedly execute one or more statements - while statement/loop - syntax while () statement - statement is usually a block statement - statement is executed as long as the condition is true - flowchart - infinite loop - program never stops - exit from containing method - commonly caused by - forgetting to update variable tested in the condition - updating the variable incorrectly - off-by-one errors - solve by thinking through simple test cases - think about what each variable is supposed to represent through time - do loop - perform the body of the loop at least once - syntax do statement while (condition); - flowchart - not as common - can always be implemented as a while loop - spaghetti code - possible to draw flowcharts that can't be programmed with loops - goto statements and corresponding labels - extremely hard to read and understand - modern programmers rarely use - not supported in Java 7.2 FOR LOOPS - common loop type i = start; while (i <= end) { ... i++; } - special form for (i = 0; i <= end; i++) { ... } - declare the loop counter variable inside the for loop header for (int i = 0; i <= end; i++) { ... } - restricts usage to within loop - general syntax for (initialization; condition; update) statement - use when a variable runs from a starting to an ending value with a constant increment or decrement - flowchart - slots may contain any expressions - bad taste to put expressions unrelated to conditions - should only initialize, test, and update a single variable - idiom for a while loop of a particular form; only use for this purpose - if run a loop without a body - must follow with an "empty statement" terminated by a semi-colon - place in a line by itself to make it stand out - don't place a semicolon after the for loop header, or it will act like this - don't use != to test the end of a range, because it assumes something about the beginning of the range or the increment that may not be correct - scope of variables defined in a for loop header - if need to use outside of the loop, must declare outside of the loop - can put multiple statements in each expression of the header by separating them with commas - less confusing to only have one control variable in the header 7.3 NESTED LOOPS - e.g. printing a table with rows and columns - body of a loop is another loop 7.4 PROCESSING SENTINEL VALUES - sentinel value - used to indicate the end of a data set - not part of the data - be careful to choose something that couldn't be a real value - loop and a half - if termination condition can only be evaluated in the middle of a loop, use a boolean flag to control that loop - implementing loops - list the work that needs to be done in every step of the loop body - find out how often the loop is repeated - for loop is usually appropriate if loop is executed a definite number of times - while loop if don't know how many times body will be repeated - for a while loop, find out where you can determine that the loop is finished - before - may never be executed - while(condition) - middle: loop and a half - end - always executes at least once - do/while loop - put operation into body - for loop usually uses index - double check variable initializations - check for off-by-one errors - consider the simplest possible scenarios - verify results if - never executed - executed exactly once - symmetric and asymmetric bounds - symmetric bounds: equality on both sides - asymmetric bounds: one side equal but other side not - don't force symmetric - determine bounds - count iterations - easier with asymmetric bounds - i = a; i < b - executed b - a times - symmetric bounds - i = a; i <= b - executed b - a + 1 times - fence post error: fence with 10 sections has 11 posts - increment other than one - symmetric: (b - a) / c - asymmetric: (b - a) / c + 1 - loop and a half problem - combine assignment and test in loop condition - expression with a side-effect - generally bad style because hard to read and maintain - exit the loop in the middle - return statement - break statement - break and continue statements - break - exit a loop - generally a poor way of exiting a loop - cannot be used to exit an if statement - difficult to use correctness proof techniques - some programmers find it useful for loop and a half - break label; - break out of a nested statement - jump to the end of the statement tagged with label - tag statement with label - syntax: label: statement - recommended use additional methods instead of complicated nested loops - continue - jump to the end of the current iteration of the loop - rarely used 7.5 RANDOM NUMBERS AND SIMULATIONS - simulation - generate events - evaluate outcome - randomized in book - random number generator - Random class - nextInt(n): 0 <= retval < n - nextDouble(): 0 <= retval < 1 - actually pseudorandom