Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / lecture2_2

.pdf
Скачиваний:
34
Добавлен:
14.10.2016
Размер:
355.69 Кб
Скачать

Enhanced for Loop with ArrayLists

names (ArrayList of String types)

George

 

Jill

 

Xinyi

Ravi

Loop accesses

Each iteration returns

the next element of the

each element of

ArrayList in name.

ArrayList in turn.

 

for (String name : names ) { System.out.println("Name is " + name);

}

9 - 21

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Using break with Loops

break example:

int passmark = 12;

 

boolean passed = false;

 

int[] score = { 4, 6, 2, 8, 12, 34, 9 };

 

for (int unitScore : score ) {

 

if ( unitScore > passmark ) {

There is no need to go

through the loop again,

 

 

 

 

passed = true;

 

 

 

so use break.

 

 

 

 

break;

 

 

}

 

 

 

 

 

 

}

System.out.println("One or more units passed? " + passed);

Output:

One or more units passed? true

9 - 22

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Using continue with Loops

continue example:

int passMark = 15; int passesReqd = 3;

int[] score = { 4, 6, 2, 8, 12, 34, 9 }; for (int unitScore : score ) {

if (score[i] < passMark) { continue;

}

passesReqd--;

// Other processing

If unit failed, go on to check next unit.

}

System.out.println("Units still reqd " + Math.max(0,passesReqd));

9 - 23

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Topics

Create a while loop

Develop a for loop

Nest a for loop and a while loop

Use an array in a for loop

Code and nest a do/while loop

Compare loop constructs

9 - 24

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Coding a do/while Loop

Syntax:

do {

code_block;

}

while (boolean_expression); // Semicolon is mandatory.

9 - 25

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Coding a do/while Loop

setFloor() {

//Normally you would pass the desiredFloor as an argument to the

//setFloor method. However, because you have not learned how to

//do this yet, desiredFloor is set to a specific number (5)

//below.

int desiredFloor = 5;

do {

if (currentFloor < desiredFloor) { goUp();

}

else if (currentFloor > desiredFloor) { goDown();

}

}

while (currentFloor != desiredFloor);

}

9 - 26

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Topics

Create a while loop

Develop a for loop

Nest a for loop and a while loop

Use an array in a for loop

Code and nest a do/while loop

Compare loop constructs

9 - 27

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Comparing Loop Constructs

Use the while loop to iterate indefinitely through statements and to perform the statements zero or more times.

Use the do/while loop to iterate indefinitely through statements and to perform the statements one or more times.

Use the for loop to step through statements a predefined number of times.

9 - 28

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Quiz

________________ enable you to check and recheck a decision to execute and re-execute a block of code.

a.Classes

b.Objects

c.Loops

d.Methods

9 - 29

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Quiz

Which of the following loops always executes at least once?

a.The while loop

b.The nested while loop

c.The do/while loop

d.The for loop

9 - 30

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Соседние файлы в папке Lectures