Key Concepts

Review core concepts you need to learn to master this subject

For-each statement in Java

// array of numbers int[] numbers = {1, 2, 3, 4, 5}; // for-each loop that prints each number in numbers // int num is the handle while numbers is the source array for (int num : numbers) { System.out.println(num); }

In Java, the for-each statement allows you to directly loop through each item in an array or ArrayList and perform some action with each item.

When creating a for-each statement, you must include the for keyword and two expressions inside of parentheses, separated by a colon. These include:

  1. The handle for an element we’re currently iterating over.

  2. The source array or ArrayList we’re iterating over.

Learn Java: Loops
Lesson 1 of 1
  1. 1
    In the programming world, we hate repeating ourselves. There are two reasons for this: - Writing the same code over and over is time-consuming. - Having less code means having less to debug. But w…
  2. 2
    A while loop looks a bit like an if statement: while (silliness > 10) { // code to run } Like an if statement, the code inside a while loop will only run if the condition is true. However, a…
  3. 3
    When looping through code, it’s common to use a counter variable. A counter (also known as an iterator) is a variable used in the conditional logic of the loop and (usually) incremented in valu…
  4. 4
    Incrementing with loops is actually so common in programming that Java (like many other programming languages) includes syntax specifically to address this pattern: for loops. A for loop header…
  5. 5
    Even though we can write while loops that accomplish the same task, for loops are useful because they help us remember to increment our counter — something that is easy to forget when we increment …
  6. 6
    One common pattern we’ll encounter as a programmer is traversing, or looping, through a list of data and doing something with each item. In Java, that list would be an array or ArrayList and the …
  7. 7
    If we ever want to exit a loop before it finishes all its iterations or want to skip one of the iterations, we can use the break and continue keywords. The break keyword is used to exit, or break,…
  8. 8
    Sometimes we need access to the elements’ indices or we only want to iterate through a portion of a list. If that’s the case, a regular for loop or while loop is a great choice. For example, we c…
  9. 9
    If we want to remove elements from an ArrayList while traversing through one, we can easily run into an error if we aren’t careful. When an element is removed from an ArrayList, all the items that…
  10. 10
    Nice work! Let’s iterate over what you’ve just learned about loops: - while loops: These are useful to repeat a code block an unknown number of times until some condition is met. For example: int…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo