Doubt with the code to add and extract the factorial in Java

5

I'm starting with the Java programming language and I'm very green, the first exercise already brings me a headache, I have a problem to solve with this code, because I do not know how it really works, I had to add all the numbers from 1 at 100 and I did this

public static void main(String[] args) {
    int suma = 0;
    for(int i = 1; i <= 100; i++){
        suma = suma + i;
    }
    System.out.println(suma);
}

The exercise goes well, but I do not understand how it works, the loop in if I have it clear, but the sum = sum + i brings me head because I do not know how it does, lowers the number of the loop aiy then add it with the 0 of the int sum? If I change the int sum = 0 for int sum 1 it gives me a result of 5051, why does it add 1 more? I have no idea, I leave another example taking the factorial of 5:

public static void main(String[] args) {
    //Factorial de 5!
    int factorial = 1;
    for (int i = 1; i <= 5; i++) {
        factorial = factorial * i;
    }
    System.out.println(factorial);
}

In this same doubt, and if I change the factorial int to 0 the result is 0, multiply all by 0?

Anyone who solves this doubt?

Thanks in advance

    
asked by DrWho 14.10.2016 в 13:21
source

2 answers

3

A for loop is an iteration. When you indicate:

int suma = 0
int factorial = 1

What you do is initialize these variables, that is, give them an initial value.

A loop for works in the following way. You have three parameters.

  • The first indicates where the iteration starts
  • The second parameter how many times the iteration will repeat
  • The third parameter is going to be a counter

In each iteration, when you put i++ the variable i increases. Therefore in your loop I will do the first three iterations:

  • When the loop enters the sum is equal to 0.
  • The variable i is equal to 1 (you have indicated it in the first parameter)
  • suma = suma + i Add the value you have currently stored in the variable suma plus the current value of the variable i

For the first three iterations:

suma = suma + i; //suma = 0 + 1   --> suma = 1
suma = suma + i; //suma = 1 + 2   --> suma = 3
suma = suma + i; //suma = 3 + 3   --> suma = 6

In the same way it works for the factorial. In the factorial you can not initialize the variable to 0 because any value multiplied by zero will always return zero and therefore when you do

factorial = factorial * i

The previous value of factorial multiplied by any value of i will be equal to zero.

    
answered by 14.10.2016 / 13:35
source
2

I do not know if it helps you understand it better, but look.

public static void main(String[] args) {
        int suma = 0;
        for(int i = 1; i <= 100; i++){
            int valorQueSumo = i;// Empieza en 1, porque tu has puesto "int i = 1".
            int valorDeSumaAnterior = suma;// Empieza con valor 0.
            // En la primera pasada, valorQueSumo = 1.
            suma = valorDeSumaAnterior + valorQueSumo;

            // La variable i aumenta de 1 en 1, ya que has puesto: i++, lo que aumenta de 1 en 1.
            // Para que veas:
            /*
             * Primera pasada del bucle: 
             * valorQueSumo = 1, porque i = 1.
             * valorDeSumaAnterior = 0, porque aún suma, no ha cambiado.
             * suma = 0 + 1; la suma cambia y ahora pasa a valer 1.
             */
            /*
             * Segunda pasada del bucle:
             * Al ser la segunda pasada, se ha ejecutado i++ una vez, por tanto i ahora vale 2.
             * valorQueSumo = 2, porque i = 2.
             * valorDeSumaAnterior = 1, porque cambió a ese valor en la primera pasada.
             * suma = 1 + 2; la suma vuelve a cambiar y ahora su valor es 3.
             */
            /*
             * Tercera pasada:
             * Ten en cuenta que i++ se ejecuto otra vez mas, por tanto i = 3, y el
             * valor de suma tambien cambio a 3.
             * Por tanto:
             * valorQueSumo = 3, porque i = 3.
             * valorDeSumaAnterior = 3, es el valor resultante de la segunda pasada.
             * suma = 3 + 3; Ahora suma, pasa a tener el valor de 6.
             */
            /*
             * Cuarta pasada:
             * Ten en cuenta que i++ se ejecuto otra vez mas, por tanto i = 4, y el
             * valor de suma tambien cambio a 6.
             * Por tanto:
             * valorQueSumo = 4, porque i = 4.
             * valorDeSumaAnterior = 6, es el valor resultante de la tercera pasada.
             * suma = 6 + 4; Ahora suma, pasa a tener el valor de 10.
             */
            // Y asi, consecutivamente, hasta que i++ llegue a 101, y ya no cumple: i <= 100.
        }
        System.out.println(suma);
    }
    
answered by 14.10.2016 в 13:39