posted 24 years ago
Hi, Balaguru.
I'm not sure what kind of magic bullet you're looking for....Usually, with this kind of question, you have little choice but to put yourself in the JVM's shoes and forge ahead. But in specific cases, you may be able to apply logic to make your work easier:
For example, here, "i" is only printed if i == j and j == k. "i" will take on values of 0, 1 or 2; j will take on values of 1, 2 or 3; and k will take on values of 2, 3 or 4. Since these for loops are nested, all permutations will be subject to the if-test, and the only one where all three integers can be equal is the case where i == j == k == 2.
Other than that, just know that the innermost loop will run through all its iterations before the next-outermost loop iterates once. When I'm confronted with a question like this, and if I can't logically distill the problem to an answer quickly, I create three columns: "i", "j", and "k", and change their values as I mentally loop through the code.
Good luck, Balaguru.
Art