1 of 24

Unit 5 - Lesson 5

Enhanced For Loops

Computer Science A

2 of 24

Warm Up

3 of 24

Enhanced For Loops Anticipation Guide

📍You and your partner should have:

  • Enhanced For Loops Anticipation Guide
  • pen / pencil

Enhanced For Loops - Warm Up

CSA

4 of 24

Respond to each statement in the Before column on the Enhanced For Loops Anticipation Guide.

Enhanced For Loops - Warm Up

CSA

✅ Do This:

5 of 24

Activity

6 of 24

  • Identify the similarities and differences between using enhanced for loops with 1D arrays and 2D arrays
  • Write nested enhanced for loops to traverse a two-dimensional (2D) array

Enhanced For Loops - Activity

CSA

🎯 Lesson Objectives

By the end of this lesson, you will be able to . . .

7 of 24

How can I use enhanced for loops to traverse a 2D array?

Enhanced For Loops - Activity

CSA

🚀 Question of the Day

8 of 24

Navigate to Lesson 5, Level 1

Enhanced For Loops - Activity

  1. Predict the output of the program

There are no wrong answers!

  • Run it to compare your prediction with the results

CSA

✅ Do This:

Predict

and Run

🔮

9 of 24

Enhanced For Loops - Activity

What did you notice about�the code in this program?

What do you wonder about�the code in this program?

CSA

10 of 24

Why do you think the changes made to the loop variable affected the original object reference?

Enhanced For Loops - Activity

💡Discuss:

CSA

11 of 24

for (Dessert theDessert : myDesserts) {

theDessert.setPrice(1.50);

}

0

1

2

Dessert donut

Dessert cake

Dessert pie

Dessert theDessert

Dessert[] myDesserts =� {donut, cake, pie};

The enhanced for loop variable gets a copy of the value stored in the array.

Enhanced For Loops - Activity

📝 Unit 5 Guide

CSA

12 of 24

for (Dessert theDessert : myDesserts) {

theDessert.setPrice(1.50);

}

0

1

2

Dessert donut

Dessert cake

Dessert pie

Dessert theDessert

flavor: strawberry

price: 2.99

flavor: chocolate

price: 7.25

flavor: apple

price: 3.75

Dessert[] myDesserts =� {donut, cake, pie};

Enhanced For Loops - Activity

With objects, the value is a reference or pointer to the object in memory.

📝 Unit 5 Guide

CSA

13 of 24

for (Dessert theDessert : myDesserts) {

theDessert.setPrice(1.50);

}

0

1

2

Dessert donut

Dessert cake

Dessert pie

flavor: strawberry

price: 2.99

flavor: chocolate

price: 7.25

flavor: apple

price: 3.75

Dessert theDessert

Dessert[] myDesserts =� {donut, cake, pie};

flavor: strawberry

price: 1.50

flavor: chocolate

price: 1.50

flavor: apple

price: 1.50

Enhanced For Loops - Activity

So the enhanced for loop variable gets a copy of the reference or pointer to the object in memory.

📝 Unit 5 Guide

CSA

14 of 24

for (Dessert theDessert : myDesserts) {

theDessert.setPrice(1.50);

}

0

1

2

Dessert donut

Dessert cake

Dessert pie

flavor: strawberry

price: 2.99

flavor: chocolate

price: 7.25

flavor: apple

price: 3.75

Dessert theDessert

Dessert[] myDesserts =� {donut, cake, pie};

The enhanced for loop variable is an alias of the reference stored in the array.

flavor: strawberry

price: 1.50

flavor: chocolate

price: 1.50

flavor: apple

price: 1.50

Enhanced For Loops - Activity

📝 Unit 5 Guide

CSA

15 of 24

Navigate to Lesson 5, Level 2

Enhanced For Loops - Activity

  1. Investigate the code on Levels 2 through 4
  2. Make changes as prompted and observe the results

CSA

✅ Do This:

Investigate

and Modify

📝

16 of 24

What did you discover from the modifications you made to the code?

Enhanced For Loops - Activity

CSA

17 of 24

🎥 Enhanced For Loops and 2D Arrays

When would we use an enhanced for loop to traverse a 2D array?

Complete the guided notes on the 📝 Unit 5 Guide.

Enhanced For Loops - Activity

CSA

18 of 24

0

1

2

10

0

20

1

30

2

40

0

50

1

60

2

70

0

80

1

90

2

Console

for (int[] row : numbers) {

for (int value : row) {

System.out.print(value + " ");

}

System.out.println();

}

10

20

30

40

50

60

70

80

90

Enhanced For Loops - Activity

CSA

19 of 24

How is using an enhanced for loop with a 2D array similar to using it with a 1D array? How is it different?

Enhanced For Loops - Activity

💡Discuss:

CSA

20 of 24

Navigate to Lesson 5, Level 5

Enhanced For Loops - Activity

A

B

C

D

  • Level 5 - Check for Understanding
  • Level 6 - Practice using an enhanced for loop with a 2D array
  • Level 7 - Practice using an enhanced for loop with a 2D array

CSA

✅ Do This:

🎉 Practice

21 of 24

Wrap Up

22 of 24

Respond to each statement in the After column on the Enhanced For Loops Anticipation Guide.

Enhanced For Loops - Wrap Up

✅ Do This:

CSA

23 of 24

  • The similarities and differences between using enhanced for loops with 1D arrays and 2D arrays
  • Writing nested enhanced for loops to traverse a two-dimensional (2D) array

Enhanced For Loops - Wrap Up

🎉 Today, you learned about . . .

CSA

24 of 24

How can I use enhanced for loops to traverse a 2D array?

Enhanced For Loops - Wrap Up

🚀 Question of the Day

CSA