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

Learn Java - Loops Cheatsheet - Codecademy PDF

The document discusses for-each loops in Java. It explains that a for-each loop allows you to directly iterate through each item in an array or ArrayList. The for-each statement requires a handle for the element being iterated over and the source array or ArrayList being looped through. An example is provided showing a for-each loop that prints each number in an integer array, with the integer variable as the handle and the array as the source.

Uploaded by

abhi yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Learn Java - Loops Cheatsheet - Codecademy PDF

The document discusses for-each loops in Java. It explains that a for-each loop allows you to directly iterate through each item in an array or ArrayList. The for-each statement requires a handle for the element being iterated over and the source array or ArrayList being looped through. An example is provided showing a for-each loop that prints each number in an integer array, with the integer variable as the handle and the array as the source.

Uploaded by

abhi yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

27/04/2020 Learn Java: Loops Cheatsheet | Codecademy

Cheatsheets / Learn Java

Loops
TOPICS

For-each statement in Java


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.

// 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);
}

https://www.codecademy.com/learn/learn-java/modules/learn-java-loops/cheatsheet 1/2
27/04/2020 Learn Java: Loops Cheatsheet | Codecademy

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

You might also like