1
I have the following code:
public class Programa{
public static void main(String args[]){
int a = 10;
for (int i = 1; i<=2; i++){
for(int j=0; j<=2; j++){
if((i%2 == 0) && (j % 2 == 0)) {
a = a * 2;
} else {
a = a + 1;
}
}
}
System.out.println("O valor de a é " + a);
}
}
Everything in it I can understand perfectly, except the part of the for. Testing the program it results in 54, but how and why? I’m having a hard time playing him.
What exactly don’t you understand? You know what the operator
%
does? You know how thefor
?– Dherik
So I know that the operator % gets the rest of the division. I know that in the example of i, it starts in 1, ends in 2 and adds 1 for each time it adds up. However I cannot understand how it arrived in 54 :/
– GGirotto
Have you tried a table test of the algorithm? It may be useful also, to understand the sequence of the execution of the program, to print the variable
a
withinif
andelse
when executing the code.– Dherik
i printed the variable '-' gave 54 rsrs to trying to understand how it arrived at this value...
– GGirotto
Print inside the
if
andelse
! Will help you.– Dherik