Practice loops: counts, totals, and boundaries
1.4.3 — Practice loops: counts, totals, and boundaries
A loop repeats statements. This lesson practises counting values, adding them, and checking which values should be included.
int stores a whole number. if runs a block when its condition is true. In a for loop, the first part sets the starting value, the second checks whether to continue, and the third updates the value. Each cell includes its own inputs. Use small ranges in these exercises.
Count the values visited
Requirement: count the integers from 1 through 3, including both ends. Start the count at 0, then add 1 for every value visited.
value <= end includes end. The variable value is the number being visited; count is how many numbers have been visited.
Add the values visited
Requirement: add the integers from 1 through 3. Start total at 0 and add the current value, not 1.
| value | total after adding value |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
The next cell supplies the whole calculation again. It does not use the variables from the previous cell.
Count and add only matching values
Requirement: keep only even numbers from 1 through 5. % gives the remainder, so value % 2 == 0 is true for even numbers.
Put both updates inside the if block. Values 2 and 4 are kept: there are 2 values and their total is 6.
A range can contain no values
The next range starts at 3 and ends at 2. The first condition check is false, so the loop body never runs. Both results stay at 0.
To try a single-value range afterward, change only rangeEnd to 3. Then the loop visits 3 once: Count is 1 and Total is 3.
Repair a missing endpoint
This loop should print 1, 2, and 3, but < practiceEnd leaves out 3. Change only < practiceEnd to <= practiceEnd.
The extra condition value <= 10 bounds this practice example. Keep practiceEnd between 0 and 10.
Hint
The condition should be value <= practiceEnd && value <= 10. Leave the start and update unchanged.
Repair a missing update
The next loop prints 1 three times because current never changes. Add current = current + 1; immediately after printing it. Then the output should be 1, 2, and 3.
The attempts counter limits this starter to three repetitions even before the repair. If another cell runs endlessly, click Stop, or use Kernel → Restart kernel. Both clear runtime variables but keep your code. Fix the loop before running it again.
Hint
Keep the attempts update. Add a separate line that increases current, so the next repetition prints the next number.
Practice: choose the even numbers
The counting and addition are already supplied. Replace false in the if condition with value % 2 == 0.
Run the cell. It should print Count: 2 and Total: 6. Change only the condition.
Hint
Put value % 2 == 0 inside the parentheses after if. Both updates already belong to that block.
Worked solution
int practiceCount = 0;
int practiceTotal = 0;
for (int value = 1; value <= 5; value = value + 1) {
if (value % 2 == 0) {
practiceCount = practiceCount + 1;
practiceTotal = practiceTotal + value;
}
}
System.out.println("Count: " + practiceCount);
System.out.println("Total: " + practiceTotal);Check different range ends
In your completed cell, change only the 5 in value <= 5:
| End value | Expected Count | Expected Total |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 2 | 1 | 2 |
| 5 | 2 | 6 |
No matching values means both results remain zero.
Enable JavaScript to run the Java notebook.