LearnStanmorephysics

Grade 11 · Powering Up

REPEAT and validation

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.

Run it! When you click the button on the form, a real InputBox appears — type invalid marks and watch your validation earn its keep. The auto-marker feeds it a naughty list: 150, then -5, then 78.

What you will do

Hints
  1. The skeleton is: repeat … until (iMark >= 0) and (iMark <= 100);
  2. Log the reject inside the loop with an IF — only when the mark is out of range.
  3. 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;

About this material

This platform provides original CAPS-aligned practice material and study tools. Content is machine-verified and has not been reviewed by subject specialists. It is not affiliated with or endorsed by the Department of Basic Education. Learners should also use official past papers and consult their teachers where uncertain.