Choose loops and validate input
Match the loop to the requirement
Use FOR for a known number of accepted items, WHILE when zero iterations are possible, and REPEAT when reading at least once is required. A sentinel marks the end of input; it is not ordinary data.

Separate attempts from accepted items
To read three valid scores: the outer loop counts accepted scores; the inner REPEAT handles attempts for one score. Add to Total only after validation succeeds.
DECLARE Attempt : ARRAY[1:6] OF INTEGER
DECLARE Position, Score, Total, I : INTEGER
Attempt[1] ← 25
Attempt[2] ← 7
Attempt[3] ← -1
Attempt[4] ← 20
Attempt[5] ← 0
Attempt[6] ← 9
Position ← 1
Total ← 0
FOR I ← 1 TO 3
REPEAT
Score ← Attempt[Position]
Position ← Position + 1
UNTIL Score >= 0 AND Score <= 20
Total ← Total + Score
NEXT I
OUTPUT Total
OUTPUT Position - 1, " attempts"
The array stands in for the keyboard. 25 and -1 are rejected, so 7, 20 and 0 are accepted: the output is 27, then 5 attempts. Replace Score ← Attempt[Position] with INPUT Score; the position counter can still count attempts.

Common mistakes
- Adding to
Totalinside theREPEAT. Every invalid attempt is then counted. Add only afterUNTIL. - Writing the invalid condition after
UNTIL:UNTIL Score < 0 OR Score > 20stops at the first bad value and accepts it. - Letting a rejected attempt advance the
FORcounter.Icounts accepted scores, so an extra attempt must stay inside the inner loop. - Adding the sentinel to the total. Test for the sentinel before the value is used.
Test the decisions
For the inclusive interval 0 to 20 use -1, 0, 20 and 21 as boundary tests. An invalid attempt must neither increase the accepted-item count nor contribute to the total. Explain why UNTIL uses the valid condition, rather than the invalid condition.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A system must read at least one value and repeat until it is valid. Which loop most directly expresses this: FOR, WHILE or REPEAT?
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read integers until one is 1 to 9 inclusive. Output only the accepted value.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Đọc NumberOfScores (1 to 5). For EACH score, read repeatedly until it is 0 to 20 inclusive. Output the total of the NumberOfScores accepted scores. Do not count invalid attempts.
Nhấn Chạy để xem kết quả ở đây.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
Đã lưu cho phiên làm việc này trên trình duyệt. Tự bạn hoặc với giáo viên xem lại; các lời giải thích này không được chấm tự động.
Kiểm tra lại lời giải của bạn theo các tiêu chí sau
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.