viewing paste Unknown #9638 | Java

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
package NotYet;
 
public class SoonThough
{
    
    public static void main (String[] args)
    {
        int[] numbers = { 2, 4, 6, 8, 10} ;
    
        for( int i = 0; i < numbers.length; i++)
        {
            System.out.println(numbers[i] * 2 );
        }
    }
}
 
let's think it through
 
FIRST TIME INTERING
PRINT numbers[0] * 2 == 4
i becomes 1
1 < 5
PRINT numbers[1] * 2 == 8
i becomes 2
2 < 5
PRINT numbers[2] * 2 == 12
i becomes 3
3 < 5
PRINT numbers[3] * 2 == 16
i becomes 4
4 < 5
PRINT numbers[4] * 2 == 20
i becomes 5
5 < 5 == FALSE
so we do not enter the loop
Viewed 843 times, submitted by Guest.