Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Style.

CHECKTask 21 of 23 · step 9: Drills: check yourselfHow many times will it stop?
How many times will it stop? in practice/04/

A breakpoint on sum += i;. How many times will the program stop, what will i be, and what is sum after the loop?

Details & hint
int sum = 0;
for (int i = 0; i <= 3; i++) {
    sum += i;
}

Stuck? 4 times: i = 0, 1, 2, 3 (<=, so 3 too). After the loop sum = 0 + 1 + 2 + 3 = 6.

practice 04 main.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
#include "input.h"
int main(void) {
int grade = read_int("Grade (0 - stop): ");
int sum = 0;
int count = 0;
while (grade != 0) {
sum += grade;
count++;
grade = read_int("Grade (0 - stop): ");
}
if (count > 0) {
printf("Average: %.2f\n", (double)sum / count);
} else {
printf("No grades.\n");
}
return 0;
}