While and For loops
var i = 0; while (i < 3) { println(i); i++; } What does the code output?
0 1 2
for ( int j = 0; j < 5; j++ ) { System.out.print( j + " " ); } System.out.println( ); What does the code output
0 1 2 3 4
var i = 0; while (i <= 3) { println("hi"); } What is the output of this code?
hi hi hi hi hi ... an infinite loop
for ( int j = 10; j > 5; j-- ) { System.out.print( j + " " ); } System.out.println( ); What does the code output
10 9 8 7 6
for ( int j = 10; j > = 5; j-- ) { System.out.print( j + " " ); } System.out.println( ); What does the code output
10 9 8 7 6 5
var i = 3; while (i < 6) { println(i); i += 1; } What does the code output?
3 4 5
var x = 3; var i = 0; while (i < 3) { x += 1; i += 1; } println(x);
6
What must the test be so that the following fragment prints out the integers 5 through and including 15? for ( int j = 5; ________ ; j++ ) { System.out.print( j + " " ); } System.out.println( );
j<16
What must the test be so that the following fragment prints out the integers 5 through and including 16? for ( int j = 5; ________ ; j++ ) { System.out.print( j + " " ); } System.out.println( );
j<=16
for ( int j = 0; j < 5; j++ ) { System.out.print( " j " ); } System.out.println( ); What does the code output
j j j j j
var i = 0; while (i < 3) { println("hi"); println("bye"); i++; }
hi bye hi bye hi bye
var i = 0; while (i < 3) { println("hi"); i++; } println("bye");
hi hi hi bye
var i = 0; while (i <= 3) { println("hi"); i++; }
hi hi hi hi
var i = 0; while (i < 3) { println("hi"); i++; }
hi hi hi
var i = 0; while (i < 0) { println("hi"); } What does the code output?
It won't output anything.
What is the output of the following code fragment? for ( int j = 5; j > -5; j-- ) System.out.print( j + " " ); System.out.println( );
5 4 3 2 1 0 -1 -2 -3 -4