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