public class Solution { public static void main(String[] args) { int s = 0; int k = 2; int i = 0; for(k--; k < 10; k++) { k += 2; if(k == 6) { continue; } s += k; i++; } System.out.println("s = " + s); System.out.println("k = " + k); System.out.println("i = " + i); } } 

The above program gives the results s = 12, k = 10, i = 2, but I cannot understand its logic, that is, how it works (exactly in the loop).

  • 2
    Are you sure that I equals output 2? I do not see where in the code this variable changes. - Vlad from Moscow
  • In IntelliJ IDEA checked. It gives exactly 2. - Vladimir
  • The program is checked by launching it for execution, although without its implementation, as they say, with the naked eye it is clear that I never changes anywhere. - Vlad from Moscow

2 answers 2

Actually the output will be as follows.

 s = 12 k = 10 i = 0 

The variable i does not change anywhere in the code after initialization.

As for the other two variables, it is easy to calculate "on the fingers", to which they will be equal.

So, after initializing k value 2 is

  for(k--; k < 10; k++) { k += 2 

After k-- k became equal to 1. Further, inside the body of the cycle, it increases by 2

  k += 2; 

and after leaving the body of the cycle, it increases by 1

  for(k--; k < 10; k++) ^^^^ 

Accordingly, k successively obtains the following values ​​before checking the condition of the loop.

1, 4, 7, 10

The field of which the cycle ends

However, in the body of the loop, the variable k will have the following values

3, 6, 9

Accordingly, the variable s will be equal to the sum of 3 + 9 = 12, since the value of k equal to 6 is skipped

  if(k == 6) { continue; ^^^^^^^^ } s += k; 

EDIT : Since on the first attempt you did not manage to type the correct source code in your question, and now it is different from what it was originally, the following takes place.

There was only one change: a sentence was added to the loop that changes the value of the variable i

  for(k--; k < 10; k++) { k += 2; if(k == 6) { continue; } s += k; i++; ^^^^^ } 

Since, as was shown above, the body of the loop will be executed three times for values ​​of k respectively, equal to 3, 6, 9, and with a value of k equal to 6, the execution of the loop body is interrupted due to the contine , this sentence

  i++; 

will be executed only twice. As a result, the final value of the variable i will be 2.

  • I'm sorry. My mistake. The loop should have i ++. - Vladimir
  • @Vladimir In this case, I also cannot be equal to 2 after the program is completed. In general, nothing complicated or not understandable here. - Vlad from Moscow
  • Got it. Thank! - Vladimir

Variable s each iteration, if k not equal to 6 adds itself to the variable k At the beginning of each iteration, the variable k increased by 2 iteration continues until k less than 10

 Итог итерации 1: s = 0 + 2 - 1 + 2 = 3, k = 2 - 1 + 2 + 1 = 4, i = 0 Итог итерации 2: s = 3, k = 4 + 2 + 1 = 7, i = 0 | пропуск итерации Итог итерации 3: s = 3 + 9 = 12, k = 7 + 2 + 1 = 10, i = 0