Unit 5 - Lesson 5
Enhanced For Loops
Computer Science A
Warm Up
Enhanced For Loops Anticipation Guide
📍You and your partner should have:
Enhanced For Loops - Warm Up
CSA
Respond to each statement in the Before column on the Enhanced For Loops Anticipation Guide.
Enhanced For Loops - Warm Up
CSA
✅ Do This:
Activity
Enhanced For Loops - Activity
CSA
🎯 Lesson Objectives
By the end of this lesson, you will be able to . . .
How can I use enhanced for loops to traverse a 2D array?
Enhanced For Loops - Activity
CSA
🚀 Question of the Day
Navigate to Lesson 5, Level 1
Enhanced For Loops - Activity
There are no wrong answers!
CSA
✅ Do This:
Predict
and Run
🔮
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
Why do you think the changes made to the loop variable affected the original object reference?
Enhanced For Loops - Activity
💡Discuss:
CSA
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
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
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
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
Navigate to Lesson 5, Level 2
Enhanced For Loops - Activity
CSA
✅ Do This:
Investigate
and Modify
📝
What did you discover from the modifications you made to the code?
Enhanced For Loops - Activity
CSA
🎥 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
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
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
Navigate to Lesson 5, Level 5
Enhanced For Loops - Activity
A
B
C
D
CSA
✅ Do This:
🎉 Practice
Wrap Up
Respond to each statement in the After column on the Enhanced For Loops Anticipation Guide.
Enhanced For Loops - Wrap Up
✅ Do This:
CSA
Enhanced For Loops - Wrap Up
🎉 Today, you learned about . . .
CSA
How can I use enhanced for loops to traverse a 2D array?
Enhanced For Loops - Wrap Up
🚀 Question of the Day
CSA