How to read the for loop of the code? I guess it reads like this: For each digit of type read it char
and convert it to a character array or maybe like this: For each digit of type char
read it and add it to a character array?
System.out.println("Introduzca los digitos para la sucesion1");
for (char digito : q.nextLine().toCharArray()) {
seq1.add(digito - '0');
}
In general, after the 2 points, :
any action/condition that the cycle has to do can go for
? or are there restrictions for this different way of representing the cycle for
?
Structure of a loop
for(){}
in java:In code:
1)
two)
3)
4)
In your case:
We ask for a digit.
We prepare a for to go through x data:
In our case, when doing for(char: digit) we are going to read each character that makes up the string that is stored in digit.
There intervenes
:
these two points indicate what will be the value of the initialization (remember the form of a for).q
It will be our data scanner and it willnextLine()
return the entire string read up to the next line, that is:"Hello I'm Pepe" will be saved completely, unlike using
next()
that only Saves up to the space "Hello I'm Pepe" -> "Hello".We continue, now we will apply that saved string
toCharArray()
, this function converts that string into an array of characters.So we're saying: "For each character contained in the scanner of
nextline()
, do whatever is between the two braces."How does the for work without having an incrementer or conditional?: In the for loop, the initialization, the condition and the increment are optional instructions, but this one has an initializer that is an array, which forces the
for
to go through each one of the letters that make up said array.Next we add to
seq1
the character that is in the iteration of the for (I suppose it is an array)Translating the entire for: For each character of the character array obtained from the scanner that asked the user for the digit, add it to the array
seq1
Now for your question:
Yes and no, after initialization you can add any "conditions", taking into account that you must use logical operators like
&&(and)
and||(or)
and respecting the basic structure:As for the actions, they will go in the code block, not in the body of the
for
, that is:Examples:
No, what is meant in that for is:
Your digit variable represents each char within the array returned by q.nextLine().toCharArray() , taking them one at a time until it traverses the entire array.
The for loop you write is a special implementation of the for loop included in Java 5, called for-each .
At a high level, the syntax means:
A small example:
The result is:
If you want to know how it works in more depth, I recommend this StackOverflow question in English . As a summary, before Java 5 the implementation of for-each was done like this:
You have the advantage that the reference to each item in your collection is already created, but you lose the control variable of the traditional for loop.
Incidentally, the foreach loop can be used with any object that implements the interface
Iterable
, for example, all objects that are in the java frameworkjava.util.Collections
.