5.4.1. Loops-WE4-P1ΒΆ
Subgoals for Evaluating a Loop
Diagram which statements go together.
Define and initialize variables
Determine the start condition.
Determine the update condition.
Determine the termination condition.
Determine body that is repeated.
Trace the loop.
For every iteration of the loop, write down the values.
Loops-WE4-P1
- 27.0
- 27
- 22.5
- 22
- Compilation error
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
- 22.5
- 24.8
- 20.666667
- Compilation error
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
- 27
- 22.5
- 22
- Compilation error
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);
You have attempted of activities on this page