Preview

22 - Iteration - While Loops

 1. In while and do-while loops, a ??????? statement causes control to be transferred directly to the conditional expression that controls the loop.

  start

  continue

  break

  pause

 2. In the following code snippet, which line of code contains an error?
1. int j=0;
2. while (j<10){
3. j++;
4. if (j==5) continue loop;
5. system.out.ptintln(?j is ? +j);}

  Line 3

  Line 4

  Line 5

  Line 2

 3. The ?????????. loop is especially useful when you process a menu selection.

  switch

  do-while

  for

  for-while

 4. What is the output of the following code?
public class Main
{
	public static void main(String[] args) {
		int j=50;
while(true)
{
  if(j<10)
     break;
     j=j-10;
 }
System.out.println("j is  "+j);
	}
}

  j is 50

  j is 0

  No output

  Error

 5. Referring to the code below, the variable count is assigned a 1. The condition ( count <= 3 ) is evaluated as true.
// start count out at one
int count = 1;       

// loop while count is <= 3                           
while ( count <= 3 )                                
{
  System.out.println( "count is:" + count );    
  
  // add one to count
  count = count + 1;                               
}
System.out.println( "Done with the loop" ); 

  FALSE

  TRUE

 6. Referring to the code above, is the following statement true or false?
Because the condition is true, the block statement following the while is entirely skipped and the output directly compiled. 

  FALSE

  TRUE

 7. Again, referring to the code in question 5, How many times did the block statement following the while execute?

  1

  3

  2

  4

 8. The loop here is called a 'counting loop'. All loops in all forms have loop control variables.
int count = 0;  
int limit = 5;
while ( count <  limit )   
{
  System.out.println( "count is:" + count );
  count = count + 1;   
}
System.out.println( "Done with the loop" );      
The variable count is initialized, tested, and changed as the loo

  FALSE

  TRUE

 9. What three things must be coordinated in order for a loop to run correctly?
1. The  initial values must be set up correctly.
2. The ______ in the while statement must be correct.
The  change in variable in the loop body must be correct.

  variable loop counter

  loop body

  condition

  if statement

 10. The following program is supposed to write out the integers 10, 11, 12, 13, 14, and 15. Decide what should go in the blanks. (from top to bottom)
class Counter2
{  
  public static void main (String[] args)
  {
    int j ;

    j = ____? ;  
    while ( j <  ___?)  
    {
      System.out.println( j );  
      j = j + ___?   ;  
    }
  }
}

  10,16,1

  16,10,1

  9,16,1

  15,10,1

 11. Which statement corresponds with the following code?
int i = 1;
while
 (i < 6) {
  System.out.println(i);
  
i++
;
}

  Print i (which starts at 6) if i is less than 1

  Print 1 as long as i is less than 6 (infinite loop)

  Print i as long as i is less than 6

  Print i if i is equal to 6

 12. The following code will print "value of x :" and the values 10 to 20.

  Incorrect - it will start at 11

  Incorrect - it will only go up to 19.

  Correct

  Incorrect - it will start at 11 and go up to 20

 13. The continue statement takes the control to the beginning of the loop and body of the loop executes again. Whatever you can do with a while loop can be done with a for loop or a do while loop.
import java.util.Scanner;
 
class BreakContinueWhileLoop {
  public static void main(String[] args) {
    int n;
   
    Scanner input = new Scanner(System.in);
   
    while (true) {
      System.out.println("Input an integer");
      n = input.nextInt();
     
      if (n != 0) {
        System.out.println("You entered " + n);
        continue;
      }
      else {
        break;
      }
    }
  }
}

  FALSE

  TRUE

 14. The while loop is used for scenarios where you know exactly how many times a block of code should be executed.

  TRUE

  FALSE

 15. The do loop is similar to the while loop, except that the expression is not evaluated until after the do loop's code is executed.

  TRUE

  FALSE