Skip to main content

Data Validation

Although the while loop is the loop to choose in most circumstances, the Repeat loop is extremely useful when validating data. For example, if we wished to ensure that only marks in the range 1 to 100 were entered we could do this as follows:

 
Repeat

Write ('Enter a mark in the range 0  100 ');
Readln (Mark); 

IF (Mark < 1) OR (Mark > 100)
Then
Begin
Writeln ('Invalid mark, please reenter ');
End;

Until (Mark >= 1) And (Mark <= 100);
 

Next: Choosing a Loop Structure