Presentation is loading. Please wait.

Presentation is loading. Please wait.

all loops initialization – set up the loop

Similar presentations


Presentation on theme: "all loops initialization – set up the loop"— Presentation transcript:

1 all loops initialization – set up the loop
decision – control for when to stop loop (continue while condition is true) update – you must “approach” the false condition in some way body – the group of actions that you will take while the loop is executing Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

2 The while Loop While loop is a “pre-condition” loop
The decision comes first Example: WhileLoop.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

3 The while loop Flowchart
statement(s) true boolean expression? false Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

4 The do-while Loop The do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition. The do-while loop, more commonly called a do loop, takes the form: do{ statements }while(condition); Example: TestAverage1.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

5 The do-while Loop Flowchart
statement(s) true boolean expression? false Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

6 The for Loop The for loop is a specialized form of the while loop, meaning it is a pre-test loop. The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code. The for loop takes the form: for(initialization; test; update) { loop statements; } Example: Squares.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

7 The for Loop Flowchart statement(s) true boolean expression? false
update Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

8 The Sections of The for Loop
The initialization section of the for loop allows the loop to initialize its own control variable. The test section of the for statement acts in the same manner as the condition section of a while loop. The update section of the for loop is the last thing to execute at the end of each loop. Example: UserSquares.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

9 Running Totals Loops allow the program to keep running totals while evaluating data. Imagine needing to keep a running total of user input. Example: TotalSales.java Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.


Download ppt "all loops initialization – set up the loop"

Similar presentations


Ads by Google