repeat
writeln('please enter a number between 1 and 100:');
readln(num)
until (1 <= num) and (num <= 100)
This program works, but the prompt and the cursor are not on the same line. We can use write instead of writeln because write does not end a printed line with an end-of-line character.
If an end user does enter numbers out of the range, this program does not produce any warning or error message, it simply prompts the user to enter again. It is adviseable to print a warning or error message so the end user understands why he/she is prompted again.
The message should be printed only if the number is out of range. This implies two things. First, the error message printing logic is a part of the loop. Second, the error message printing logic should occur after the readln statement.
How do we print the error message? We can use a simple writeln statement like the following:
repeat
write('please enter a number between 1 and 100:');
readln(num);
writeln('error: your number is less than 1 or greater than 100!')
until (1 <= num) and (num <= 100)
This is wrong because the error message is printed unconditionally. It should be printed only if the number is out of range. As a result, we need to a conditional statement to ``guard'' the printing of the error message when the number is in-range. The modified code is as follows:
repeat
write('please enter a number between 1 and 100:');
readln(num);
if (num < 1) or (100 < num) then
writeln('error: your number is less than 1 or greater than 100!')
until (1 <= num) and (num <= 100)
This last modification completes this code.