For-each loop
Start course
3h 15m

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge

Hi there. In this video, we will learn about the For-Each Loop. In Java, For-Each Loop is another form of For Loop to work with a raise and collection. We can say it's the enhanced form of For Loop. The use of enhanced loop is easier to write. It makes your code more readable. If you look at the syntax, first we declare an array with initial values, then we use the For-Each Loop. Instead of the array, we can also use lists. We'll learn about lists in the next lectures. In the for parentheses, the first element is the single item from the array and the second item is the list of the array. Colon is used between them. Note that if you want to use the index of the element inside of the loop, you have to use the For Loops and can't use For-Each loops. 

In our example, it iterates through each element in the numbers variable, stores it in the number variable, and executes the loop body. Let's make some examples with the For-Each Loop. In exercise project, right click on the loop package and select "New" class. Specify the class name as For-Each Loop and select the checkbox for the main method. First, let's take an example to iterate through elements of an array using the standard for loop. Let's declare a char array. Its name is letters  and its initial values are h, e, l, l, o. Lets start to write the for loop. In the for loop, we use the variable i and we declare and initialize variable i with zero value. Our task condition is i, less than the length of the letters array. And we increase the value of i by 1. Let's print the elements of the array by using index in the print method, S.out.(letters[i]). Okay, let's run the code. 

You see the elements of the array in the console. You can perform the same example using For-Each Loop. Let's copy the declaration part of the array from the previous code and convert this code to the comment line. Let's start to write For-Each Loop. In the for parentheses, first we declare a variable item with char type because the type of letters array is char so the data type should be the same. And we add colon. And after the colon, we write the name of the array, i.e., letters. Let's add the variable item into the print method, Sout.item. Okay, let's run the code. You see elements of letters array in the console. You can see the difference between the loops. In the For-Each loop, the code has reduced significantly. 

Also, we don't use index encounter in the For-Each Loop. Now, in our example, let's take a close look at how the Four-Each Loop works. The For-Each Loop iterates through each item in the given letters array and it stores each item in the variable item and executes the body of the loop. Very nice. Notice that we always use the For-Each Loop with arrays, but now I'm going to show you a different use. First of all, let's turn the codes here into comments again. Now, let's define a string variable named language. And let's assign the word "java" to this variable. Now, let's create a For-Each Loop that will print the letters of the word "java" to the console in order. 

Notice that inside the for parentheses, we first had to create a variable of the same type as the array, but language is a variable, not in array. So, how do we define it? The "java" word, which is actually a string expression, is formed by combining the letters, J-A-V-A. In other words, we can think of the string as an array of letters. So here, we can use the char data type that represents each letter. That's why I'm writing the char letter here. After the colon, we need to write the array again, but the language we have is a variable, not an array. So, what are we going to do? Here,

I write If you notice, the code editor offers us some methods. There's a method named toCharArray. When you click on this method, it says in the description section on the right that this method will convert a string to a character array. In other words, if we use this method, we'll actually write an array with the char data type here. That's why I'm writing language.toCharArray here. And let's print the letters of Java in the loop. I write, Sout(letter). Now, let's run the code. As you can see, we have printed the letters of the word "java" to the console using the For-Each loop. Of course, if you use print instead of println here, the letters will be printed side by side, not one under the other. Let's run it one more time, and we see the word Java in the Console. Thus we learned that we can think of a string as an array of char and put it in a For-Each loop. 

Now, let's do another example. Let's write a program to calculate the sum of all elements of an integer array. We declare an integer array named numbers. And I'll initialize it with the values 6, 4, 3, and 5. And we declare a variable sum. Its initial value is zero. In for parentheses, we declare a variable num with int type, and we add a colon and numbers array. I'll assign the sum plus num to the variable sum with the add and assignment operator. So I write, sum + = num. Let's print the value of num in the for loop by using the print method. Finally, we use the print method after the for loop to display the sum of numbers, Sout "sum of numbers = + sum." Okay, let's run the code. You see the values of num and the sum of numbers is 18. Yes, we learned the usage of the for loop in Java. Let's take a short break here. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics