Grade 11 · Conditional loops · Lesson and coding task · about 14 minutes
Open this task in the Delphi workspace →
The REPEAT … UNTIL loop tests its condition at the end, so the body always runs at least once. That makes it the natural shape for input validation — ask, check, and ask again until the input is acceptable:
repeat
sInput := InputBox('Marks', 'Enter a mark (0-100):', '0');
iMark := StrToInt(sInput);
until (iMark >= 0) and (iMark <= 100);
Note the differences from WHILE: no begin…end needed (REPEAT and UNTIL fence the body themselves), and the condition says when to stop, not when to keep going.
InputBox(caption, prompt, default) pops up a small dialog and returns whatever the user types, as a String.
Your task
Keep asking for a mark until it is valid (0–100). Log every rejected attempt to the memo so the user can see their sins.
What you will do
- Use REPEAT … UNTIL to keep asking via InputBox until the mark is 0-100
- Every invalid attempt adds a memo line: Rejected: 150
- When a valid mark arrives, lblResult shows: Valid mark: 78
Hints
- The skeleton is: repeat … until (iMark >= 0) and (iMark <= 100);
- Log the reject inside the loop with an IF — only when the mark is out of range.
- Out of range means (iMark < 0) or (iMark > 100).
Model solution
procedure TfrmValid.btnEnterClick(Sender: TObject);
var
iMark : Integer;
sInput : String;
begin
repeat
sInput := InputBox('Marks', 'Enter a mark (0-100):', '0');
iMark := StrToInt(sInput);
if (iMark < 0) or (iMark > 100) then
memOutput.Lines.Add('Rejected: ' + IntToStr(iMark));
until (iMark >= 0) and (iMark <= 100);
lblResult.Caption := 'Valid mark: ' + IntToStr(iMark);
end;