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 Attempt[Position] with INPUT Score and nothing else changes.

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.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A system must read at least one value and repeat until it is valid. Which loop most directly expresses this: FOR, WHILE or REPEAT?
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read integers until one is 1 to 9 inclusive. Output only the accepted value.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read 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.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
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.
บันทึกสำหรับรอบนี้บนเบราว์เซอร์นี้ ทบทวนด้วยตนเองหรือร่วมกับครู; คำอธิบายเหล่านี้ไม่ถูกให้คะแนนอัตโนมัติ
ทบทวนคำอธิบายของคุณตามเกณฑ์เหล่านี้
- 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.