Chapter 9 Control Structures.
-
Upload
shanna-reeves -
Category
Documents
-
view
220 -
download
0
description
Transcript of Chapter 9 Control Structures.
Chapter 9
Control Structures
Learning Java through Alice © Daly and Wrigley
Objectives•Code programs using the following looping techniques: ▫While Loop ▫Do While Loop ▫For Loop
• Explain when the Break and Continue statements would be used when coding loops.
•Walk through While, Do While, and For loops documenting variables as they change.
2
Learning Java through Alice © Daly and Wrigley
while loop
While Syntax: Flowchart:
while (boolean condition) { repeated statements }
3
Learning Java through Alice © Daly and Wrigley
while loop example:
While : Output:
int counter = 1; while (counter <=5) { System.out.println ("Hello " + counter); counter ++; }
Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
4
Learning Java through Alice © Daly and Wrigley
do while loopDo While Syntax: Flowchart:
do { repeated statements } while (boolean condition);
5
Learning Java through Alice © Daly and Wrigley
do while loop example:Do While : Output:
Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
int counter = 1; do { System.out.println("Hello " + counter); counter++; } while (counter <=5);
6
Learning Java through Alice © Daly and Wrigley
for loopfor (initialization; test; increment) { statements; }
Initialization - initializes the start of the loop.
Example: int i = 0;Boolean Test - occurs before each pass of the loop.
Example: i<10;Increment - any expression which is updated at the end of each trip through the loop.
Example: i ++, j+=3 Statements are executed each time the loop is executed.
7
Learning Java through Alice © Daly and Wrigley
for loop logic
8
Learning Java through Alice © Daly and Wrigley
for loop example 1:for loop Results
for (int counter =1; counter <=5; counter++ ) { System.out.println ("Hello " + counter);}
Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
9
Learning Java through Alice © Daly and Wrigley
for loop example 2:for loop with modulus results
for (int number = 0; number <1000; number++) { if ( number % 12 = = 0 ) { System.out.println("Number is " + number); }}
..... etc. Number is 996
10
Learning Java through Alice © Daly and Wrigley
for loop example 3:for loop to print even numbers from 1 to 20 results
for (int number = 2; number <=20; number+=2) { System.out.println("Number is " + number); }
The number is 2The number is 4The number is 6The number is 8The number is 10The number is 12The number is 14The number is 16The number is 18The number is 20
11
Learning Java through Alice © Daly and Wrigley
for loop example 4:for loop with multiple initialization & increments results
for (int i=0, j=0 ; i*j < 1000; i++, j+=2) { System.out.println( "The answer is " + i + " * " + j + " = " + i*j ); }
12
Learning Java through Alice © Daly and Wrigley
Break Statement
for (int index =1; index<= 1000; index ++) { if (index == 400) { break; } System.out.println("The index is " + index); }
13
Learning Java through Alice © Daly and Wrigley
Continue Statement
for (int index = 1, index <= 1000; index ++) { if (index == 400) { continue; } System.out.println("The index is " + index); }
14