Value of j increases whenever value of i isn't an even number
After the first run-through of the outerloop, each time it iterates after that, the value of i is one more than it was the previous iteration. And with each iteration of the outerloop, the innerloop starts from the beginning - being newly initialized - and j starts with a value of 2 each time. This is because, in order to determine that a number isn't prime, it must be divisible by 2 with no remainder. Each time the outerloop iterates (and the innerloop executes anew) if i is an even number, j doesn't increase beyond its initialized value of 2 becasue the code of the if-statement gets executed.
Each time the innerloop iterates, and when i isn't an even number, j (which starts each innerloop iteration with a value of 2) is increased by one and divided into i. If the remainder, or modulus, of each division operation is ever zero, the innerloop is abandoned and the System.out.println(i) line of code, which is part of the outerloop, isn't executed. As long as i isn't an even number (and obviously divisible by 2), j will increase during iterations of the innerloop until it either divides into i with the modulus being zero (as in the case of when i has value of 9 and j increases to 3) or until j increases to the same value as i (as in when i is 5 and j increases from 2 to 3 to 4 then to 5, ending the current series of iterations of innerloop, with the modulus never being zero).
Last edited by skf001; August 4th, 2012 at 09:51 AM..
|