What are the syntax of the for loop in java?

Asked

Viewed 17,877 times

11

Hello!

Everyone programming in Java should be tired of using loops for, that almost always has a syntax similar to this:

for (int x = 0; x < y; x++) {
    ...
}

I was analyzing some codes and came across a different syntax:

for (String novaString : arrayDeStrings) {
    ...
{ 

From what I understand, the loop will repeat X times, where X is equal to the size of the array of strings (arrayDeStrings.length) and with each repetition, novaString will receive the contents of one of the strings of arrayDeStrings.

Doubts

Is this really how this second syntax presented works? Are there other syntaxes besides those mentioned in this question? If so, how do they work?

2 answers

15


This is a loop mode equivalent to for ... each of other languages.

As you suspected, it iterates the array arrayDeStrings, sequentially picking up each of its items and returning in novaString, running the inside of the loop with the value of each item respectively.

Imagine the following array of Strings:

String[] arrayDeStrings = { "Banana", "Maçã", "Pera" };

With this array, doing so much:

// Sintaxe foreach
for (String novaString : arrayDeStrings) {
   System.out.println( novaString );
}

how much so:

// Sintaxe for com iterador
for (Iterator<String> i = arrayDeStrings.iterator(); i.hasNext(); ) {
   String novaString = i.next();
   System.out.println( novaString );
}

or so:

// Sintaxe for convencional
for(int i = 0; i < arrayDeStrings.length; i++) {
   System.out.println( arrayDeStrings[i] );
}

You will get the expected result:

Banana
Maçã
Pera

Note that the foreach is just one syntax-sugar, as appropriate comment from @mgibsonbr, it will be transformed into one of the two codes that were exposed next, depending on whether it is a Iterable or Array in the parameter.

  • 1

    Right, so that’s how it works. There are other similar?

  • 1

    @Deyel by coincidence, I just posted.

  • 1

    Wow, great! I’ll wait a while before giving the best answer, to see if other people know other syntaxes...

  • @Deyel is not in a hurry, there is certainly a good people in Java here (and java is not really my thing). Things can appear much more elaborate!

  • yeah, I’m also not pro at java haha. Anyway thanks for the clarification!

  • 2

    +1 and I would add that the second code (using Iterator) is basically what the compiler will generate when it finds the.. in applied to a Iterable and - second that answer in Soen - the second code is what it will generate when applied to an array.

  • @mgibsonbr So you mean that the first syntax presented in the answer was only created to facilitate the use of the second? (mask the complexity)

  • 2

    @Deyel Yes, the second and the third. As far as I know, it is only a "syntactic sugar" even, it does not add anything that could not be done without it. But it certainly helps a lot in the clarity and conciseness of the code.

  • Personally I think the second case is best expressed with a while and not a for. And no, there are no other syntaxes of for, if that’s what you want to know.

  • @Piovezan agree, but as the question was about syntax of For, I stayed within the "theme" :)

Show 5 more comments

3

This type of construction started with Java 5. It is called for-each and does just this: for every element given in loop of for, executes one or more commands.

With it the reading of the code becomes easier, and internally it is compiled for the same bytecodes that the for or the while correspondents would make.

It can be used in arrays, Collections and any other construction that implements an interface Iterable.

There are situations where you will not be able to use this for:

  • to update values of Array: it only reads the array values, you cannot use this for to update its values. For example, the code below is invalid:
for (String novaString : arrayDeStrings) {     
 novaString = "banana";     
}
  • to traverse two structures at the same time: the for runs through only one structure. There is no construction of this for that allows you to traverse two arrays comparing their values, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.