AP® Computer Science A glossary

Enhanced for loop

Also called: for-each loop

Course: AP® Computer Science A Shows up in: Unit 4: Data Collections

A loop that assigns a copy of each element of an array or ArrayList, in order, to its loop variable, without using an index.

Appears in the course and exam description (CED): 4.4.A.34.4.A.44.4.A.54.4.A.64.9.A.4

Assigning to the loop variable does not change the array. With object references, calling a method on the loop variable can change the object. Adding or removing ArrayList elements inside it can throw a ConcurrentModificationException.

int[] marks = {7, 9};
for (int m : marks)
{
    m = 0;
}
System.out.println(marks[0] + marks[1]);

Output:

16
Learn it properly, with a checkpoint 4.4 Array Traversals

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure