Presentation is loading. Please wait.

Presentation is loading. Please wait.

Follow up from lab See Magic8Ball.java Issues that you ran into.

Similar presentations


Presentation on theme: "Follow up from lab See Magic8Ball.java Issues that you ran into."— Presentation transcript:

1 Follow up from lab See Magic8Ball.java Issues that you ran into.

2 Loops CODE EXAMPLES LOOPY.JAVA

3 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 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?

5 The while loop Flowchart 4-5 statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body. while (result < 10) { ….. do something } while loops are indeterminant loops, we execute until a condition is met and not for any pre – determined number of times.

6 The while loop Flowchart 4-6 statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.

7 The while loop Flowchart 4-7 statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.

8 The while loop Flowchart 4-8 statement(s) true boolean expression? false Another name is precondition loop – You evaluate the condition before executing the body.

9 The do - while Loop Flowchart 4-9 statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body. do { … statements } while (condition);

10 The do - while Loop Flowchart 4-10 statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.

11 The do - while Loop Flowchart 4-11 statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.

12 The do - while Loop Flowchart 4-12 statement(s) true boolean expression? false Another name is post condition loop – You evaluate the condition after executing the body.

13 The for Loop Flowchart 4-13 statement(s) true boolean expression? false update Another name is counted loop – You typically will execute a for loop based on some counter. A for loop is also a precondition loop. In a for loop, the initialization, boolean expression and update are combined in one structure. for (int ii = 0; ii < 10; ii++) { … statements }

14 Code example Loopy.java Some terms ◦loop control variable ◦precondition loop ◦post condition loop ◦counted loop


Download ppt "Follow up from lab See Magic8Ball.java Issues that you ran into."

Similar presentations


Ads by Google