On different interpretations of the for cycle

0

How to read the for cycle of the code? I guess it reads like this: For each digit of type char read it and convert it to an array of characters or maybe like this: For each digit of type char read it and add it to an array of characters?

 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 : can any action / condition that the cycle for have to do? or are there restrictions for this different way of representing the cycle for ?

    
asked by Michelle 28.08.2017 в 04:53
source

3 answers

2

Structure of a cycle for(){} in java:

In code:

for(inicialización;condicion;incremento)
{
    //instrucciones
}

1)

  

Execution of the for control structure begins with the    initialization instruction. This instruction usually performs the   initialization of a control variable that is usually known as    sentinel .

2)

  

Next we check the condition whose result has to be   a Boolean value of true or false.

3)

  

If the condition evaluates to true, the block is executed   instructions delimited by the keys that are only necessary if   There is more than one instruction.

4)

  

Then the increment instruction is executed and checked again   the condition. So on until the condition is not met.

In your case:

We ask for a digit.

System.out.println("Introduzca los digitos para la sucesion1");

We prepare a for to travel x data:

for(char digito:){
}

In our case when doing for (char: digit) we will read each character that compose the string that is saved in digit.

Here there is : , these two points indicate what the value of the initialization (remember the shape of a for).

for(char digito: q.nextLine()){
}

q will be our data scanner and nextLine() will return all the string read to the next line, that is:

"Hi, I'm pepe" will be saved completely, unlike using next() that only Save up to the "Hello, I'm pepe" - > "Hello."

We continue, now that saved string will apply toCharArray() , this function converts that string into an array of characters.

for(char digito: q.nextLine().toCharArray()){
}

Then we are saying: "For each character contained in the nextline() scanner, do what is between the two keys."

How does the for to work without having incremental or conditional?: In the for cycle, initialization, condition and increment are optional instructions, but this has an initializer that is an array , which forces the for to go through each of the letters that make up that array.

Next we add to seq1 the character that is in the iteration of the (I assume it is an array)

for (char digito : q.nextLine().toCharArray()) {
    seq1.add(digito - '0');
}

Translating all the for: For each character of the character array obtained from the scanner that requested the user's digit, add it to the array seq1

Now, for your question:

Yes and no, after the initialization you can add any "condition", taking into account that you must use logical operators like &&(and) and ||(or) and respecting the basic structure:

for(inicializacion ; aqui || tus && condiciones ; contador){}

As for the actions, they will go in the code block, not in the body of for , that is:

for(int i=0; i<a || i<b || i>c; i++){
    // AQUI 
    int x = i + 100;
    int z = i x 2;
}

Examples:

for (int i = 1, j = 100; i <= 100 && j > 0; i = i - 1 , j = j-1) {
    System.out.println("Inside For Loop");
}

for( int i = 0 ; i < 100 || otraCondicion() ; i++ ) {
  System.out.printLn("Inside another For Loop");
}
    
answered by 28.08.2017 / 05:07
source
1

No, what is meant in that for is:

//  Para cada char en el siguiente array de chars.
for (char digito : q.nextLine().toCharArray()) {  
    seq1.add(digito - '0');  //  has lo siguiente.
}

Your digito variable represents each char within the array that returns q.nextLine (). toCharArray () , taking one by one until you scroll through the whole .

    
answered by 28.08.2017 в 04:59
1

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:

  

For each variable of type char , which we will call digito , which is included in the resulting array of converting a String to an array of char :    (Logic of the loop)

A small example:

//un array cualquiera
int[] listaDeNumeros = {1, 2, 3, 4};   

System.out.println("For-each");
for (int numero : listaDeNumeros) {
    System.out.println("Numero: " + numero);
}

System.out.println("For tradicional");
for (int i = 0; i < listaDeNumeros.length; i++) {
    System.out.println("Numero: " + listaDeNumeros[i]);
}

The result is:

For-each
Numero: 1
Numero: 2
Numero: 3
Numero: 4
For tradicional
Numero: 1
Numero: 2
Numero: 3
Numero: 4

If you want to know how it works in greater depth, I recommend this question of StackOverflow in English . To summarize, before Java 5 the implementation of for-each was done like this:

Iterator<Integer> iterador = listaDeNumeros.iterator();
int numero;
while(iterador.hasNext())  {
    numero = iterador.next();
}

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 framework java.util.Collections .

    
answered by 30.08.2017 в 17:05