Good morning, a question, what does the following lines of code do?
public static void main (String[] ars) {
int suma = 0;
for (int x=1;x<=100;x++) {
if (x%2!=0) suma+=x;
}
}
Good morning, a question, what does the following lines of code do?
public static void main (String[] ars) {
int suma = 0;
for (int x=1;x<=100;x++) {
if (x%2!=0) suma+=x;
}
}
Here's an explanation of what the code does:
In this case, the numbers of the series will be added:
What is being executed in those lines of code is that the variable "x" that is declared inside the parentheses of the "for" takes the values from 0 to 100 and after "x" assumes a value, it asks if the number is not even and since it is not, it adds to the variable "sum" the value that "x" has at that moment. That is the function of "for" to traverse a range, making a variable in this case "x" take all the values from the beginning of the range to the end.