Loops Code examples Loopy java TAP Result Discussion














- Slides: 14
Loops Code examples Loopy. java
TAP Result Discussion
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 boolean expression? true statement(s) false 4 -5 Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart boolean expression? statement(s) true false 4 -6 Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart boolean expression? statement(s) true false 4 -7 Another name is precondition loop – You evaluate the condition before executing the body.
The while loop Flowchart boolean expression? true statement(s) false 4 -8 Another name is precondition loop – You evaluate the condition before executing the body.
The do-while Loop Flowchart statement(s) boolean expression? true false 4 -9 Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) boolean expression? true false 4 -10 Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) boolean expression? true false 4 -11 Another name is post condition loop – You evaluate the condition after executing the body.
The do-while Loop Flowchart statement(s) boolean expression? true false 4 -12 Another name is post condition loop – You evaluate the condition after executing the body.
The for Loop Flowchart boolean expression? false 4 -13 true statement(s) update 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