Skip to main content

Section 6.8 Loops-WE4-P1

Subgoals for Evaluating a Loop.

  1. Identify loop parts
    1. Determine start condition
    2. Determine update condition
    3. Determine termination condition
    4. Determine body that is repeated
  2. Trace the loop
    1. For every iteration of loop, write down values

Subsection 6.8.1

Exercises Exercises

1.
    Q11: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1
    System.out.println("Enter a negative score to signal the end of the input.");
    int total = 0, count = 0;
    int score;
    System.out.println("Score: ");
    Scanner get = new Scanner(System.in);
    score= get.nextInt();
    while (score >= 0) {
       count++;
       total += score;
       System.out.print("Score: ");
       score = get.nextInt();
    }
    System.out.println( (total * 1.0) / count);
    
  • 27.0
  • 27
  • 22.5
  • 22
  • Compilation error
2.
    Q12: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1
    System.out.println("Enter a negative score to signal the end of the input.");
    int total = 0, count = 0;
    int score;
    System.out.println("Score: ");
    Scanner get = new Scanner(System.in);
    score= get.nextInt();
    while (score >= 0) {
       System.out.print("Score: ");
       score = get.nextInt();
       count++;
       total += score;
    }
    System.out.println( (total * 1.0) / count);
    
  • 27.0
  • 22.5
  • 24.8
  • 20.666667
  • Compilation error
3.
    Q13: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1
    System.out.println("Enter a negative score to signal the end of the input.");
    int total = 0, count = 0;
    int score;
    System.out.println("Score: ");
    Scanner get = new Scanner(System.in);
    score= get.nextInt();
    while (score >= 0) {
       total += score;
       System.out.print("Score: ");
       score = get.nextInt();
       count++;
    }
    System.out.println( (total * 1.0) / count);
    
  • 27.0
  • 27
  • 22.5
  • 22
  • Compilation error
You have attempted of activities on this page.