Skip to main content

Section 5.8 Loops-WE4-P1

Subsection 5.8.1 Subgoals for Evaluating a Loop

  1. Diagram which statements go together.

  2. Define and initialize variables

    1. Determine the start condition.

    2. Determine the update condition.

    3. Determine the termination condition.

    4. Determine body that is repeated.

  3. Trace the loop.

    1. For every iteration of the loop, write down the values.

Subsection 5.8.2 Loops-WE4-P1

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 scroe 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 scroe 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 scroe 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.