The FOR loop for Arrays: the for each loop

Throughout you Java programmer career, you will use the common for control statement to iterate through all the elements of an Array.

If this is so important and used, it should have a shortcut or easier way to do this in Java, don't you think?
And yes, there is a simple way.
Not to mention that with conventional arrays and for loop, it is quite easy to roll up and make mistakes.

Learn a special way to use the loop and stop having problems: foreach loop

Making you go throughout the Array

The usage is very simple and straightforward. Let's use this for variant that always go from beginning to end, for all elements of an Array.
It is very helpful, too, in terms of organization and precaution because some programmers do not like to use the index 0, use direct index 1 of the array, or sometimes we confuse ourselves and we go over the  limit of the array (during the iterations). This can be easily avoided by using the modified loop to iterate through the elements of an array.

The syntax for each is as follows:
for (type variableNameOfYourArray: yourArray) {
 / / Your code

If the 'yourArray' for integer, would be like this:
for (int count: yourArray) {

How can we interpret this foreach loop?
Very simple, the variable 'count' will receive, at each iteration, all values of ​​'yourArray'.
That is, it will go through your entire Array and receive their values ​​in order (from beginning to end), one at a time. And with these values ​​you will do what you want in the loop.

For example, suppose that the notes of each student in a school is stored in an Array.
How to calculate the average you always have to go all the Array / Vector, you can, and should, use this particular loop. Because it was made for this, to make our life easier.

Programmers should not waste time. They always make things as simple and practical as possible. Do not try to make art or workaround.
If  Java provides tools and options, use them.

It is a weapon 'special' is not seen around in any programming language.
If you've programmed or program in another language must be realizing how Java is full of these tools and details.
If you study everything right, will become an efficient programmer, because Java was actually done to make life easier for developers, is really a language-oriented to work.

Foreach example of use

Create a Java application that asks the user to 5 numbers.
Then show the sum of these numbers;

Step 1:
We will declare the 'number' array of integers and the variable integer 'sum' to receive the sum of the results that the user provides.

We use a for loop, conventional, to traverse the array.
Note that we use 'number.length', which is a good practice.

Step 2:
Now we have to go through the entire array, and since we do this, we do using the modified loop that traverses a whole array getting their values, the foreach.
As the variable 'count' will receive the data from all elements of the array 'number', we add the value of 'count' in every iteration variable 'sum'.

The Java code of our example will be:
import java.util.Scanner;

public class forToArrays {
    public static void main(String[] args){
            int[] number = new int[5];
            int sum=0;
            Scanner inpuy = new Scanner(;
            for(int count=0 ; count< number.length ; count++){
                System.out.print("Type the number "+(count+1)+": ");
            //displaying and adding
            for(int cont : number){
                sum += count;
            System.out.println("The sum numbers is "+ sum);


No comments: