Syntax
java
for (dataType element : array) {
// use element — it holds the current value
}
java
int[] scores = {90, 85, 77, 92, 88};
for (int score : scores) {
System.out.println(score);
}
90
85
77
92
88
How it works
java
// Enhanced for loop
for (int score : scores) {
System.out.println(score);
}
// Is equivalent to this standard for loop:
for (int i = 0; i < scores.length; i++) {
int score = scores[i]; // copy into local variable
System.out.println(score);
}
Key examples
Sum all elements
java
int[] nums = {10, 20, 30, 40, 50};
int sum = 0;
for (int n : nums) {
sum += n;
}
System.out.println("Sum: " + sum); // 150
Find the maximum
java
int[] values = {34, 67, 23, 89, 45};
int max = values[0];
for (int val : values) {
if (val > max) {
max = val;
}
}
System.out.println("Max: " + max); // 89
Count matching elements
java
int[] grades = {95, 82, 91, 78, 96, 88};
int countA = 0;
for (int g : grades) {
if (g >= 90) {
countA++;
}
}
System.out.println("A grades: " + countA); // 3
With String arrays
java
String[] names = {"Alice", "Bob", "Charlie"};
for (String name : names) {
System.out.println("Hello, " + name + "!");
}
The copy trap (cannot modify array elements)
java
int[] nums = {1, 2, 3, 4, 5};
for (int n : nums) {
n = n * 2; // only changes the local copy!
}
// Array is UNCHANGED
for (int n : nums) {
System.out.print(n + " "); // 1 2 3 4 5
}
java
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i] * 2; // this changes the actual array
}
// Now: 2 4 6 8 10
Exception: Object references
java
// If Student has a setGrade method:
Student[] students = { ... };
for (Student s : students) {
s.setGrade(100); // this DOES modify the actual Student objects
}
java
for (Student s : students) {
s = new Student("New"); // only changes local copy — array unchanged
}
When to use which loop
Examples of when you NEED a standard for loop
java
// Need index to modify elements
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
}
// Need to go backward
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
// Need to compare adjacent elements
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
System.out.println("Duplicate found!");
}
}
// Need to access a specific index
for (int i = 0; i < arr.length; i += 2) {
System.out.println("Even index: " + arr[i]);
}
Complete example: Grade report
java
public class GradeReport {
public static void main(String[] args) {
int[] scores = {92, 85, 78, 95, 88, 67, 91};
// Calculate average (enhanced for — just reading)
int total = 0;
for (int s : scores) {
total += s;
}
double average = (double) total / scores.length;
// Count above average
int aboveAvg = 0;
for (int s : scores) {
if (s > average) {
aboveAvg++;
}
}
System.out.println("Average: " + average); // 85.14...
System.out.println("Above average: " + aboveAvg); // 4
// Build pass/fail labels (need index — use standard for)
String[] labels = new String[scores.length];
for (int i = 0; i < scores.length; i++) {
labels[i] = scores[i] >= 70 ? "Pass" : "Fail";
}
}
}
AP Exam Tips
- •
- •
- •
- •
- •
- •