0% found this document useful (0 votes)
1 views

Learn Java_ Loops Cheatsheet _ Codecademy

This document is a cheatsheet for learning about loops in Java, specifically the for-each statement. It explains how to loop through an array or ArrayList using the for-each syntax, which includes a handle for the current element and the source array. An example code snippet demonstrates how to print each number in an array using a for-each loop.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Learn Java_ Loops Cheatsheet _ Codecademy

This document is a cheatsheet for learning about loops in Java, specifically the for-each statement. It explains how to loop through an array or ArrayList using the for-each syntax, which includes a handle for the current element and the source array. An example code snippet demonstrates how to print each number in an array using a for-each loop.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1/31/25, 12:30 PM Learn Java: Loops Cheatsheet | Codecademy

Cheatsheets / Learn Java

Loops

For-each statement in Java

In Java, the for-each statement allows you to directly // array of numbers


loop through each item in an array or ArrayList and
int[] numbers = {1, 2, 3, 4, 5};
perform some action with each item.
When creating a for-each statement, you must
include the for keyword and two expressions inside of // for-each loop that prints each number
parentheses, separated by a colon. These include: in numbers
1. The handle for an element we’re currently
iterating over. // int num is the handle while numbers is
2. The source array or ArrayList we’re iterating the source array
over. for (int num : numbers) {
System.out.println(num);
}

Print Share

https://www.codecademy.com/learn/learn-java/modules/learn-java-loops/cheatsheet 1/1

You might also like