Seating “chart” Front - Screen 3 2 4 4 rows 1 5 6 Back DOOR
Follow up from lab See Magic8Ball.java “solution” Issues that you ran into.
Code examples Loopy.java Loops Code examples Loopy.java
Why loops? Where have we wanted to use a loop? What kinds of problems lend themselves to iteration? What do Java loop structures look like?
4 parts to a loop Initialization – getting it ready Decision – Do I continue the loop – boolean expression Body – What do I want to accomplish in a loop Update – How do I approach the false condition to exit the loop?
The while loop Flowchart statement(s) true boolean expression? false while loops are indeterminant loops, we execute until a condition is met and not for any pre – determined number of times. while (result < 10) { ….. do something } Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.
The do-while Loop Flowchart statement(s) true boolean expression? false do { … statements } while (condition); Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.
The for Loop Flowchart statement(s) true boolean expression? false update for (int ii = 0; ii < 10; ii++) { … statements } In a for loop, the initialization, boolean expression and update are combined in one structure. Another name is counted loop – You typically will execute a for loop based on some counter. A for loop is also a precondition loop.
Code example Loopy.java Some terms loop control variable precondition loop post condition loop counted loop