Implementation (algorithm specification)Examples for input validation

Algorithms are created to allow users to tell a computer how to solve a problem. Understanding how to construct five of the most common algorithms is a critical skill for budding software developers.

Part ofComputing ScienceSoftware design and development

Examples for input validation

Visual Studio 2010 (Similar to VB5, VB6 and Subsequent Visual Basic.NET languages)

For counter = 0 To 4
score(counter) = InputBox("Please enter a score between 0 and 50", "Score Entry")
While score(counter) ˂ 0 Or score(counter) ˃ 50
score(counter) = InputBox("You have not entered a score between 0 and 50. Please try again.", "Score Validation")
End While
Next

LiveCode

Remember - Live code will index from 1 rather than 0!

repeat with loop = 1 to 5
ask "Please enter your score between 0 and 50"
put it into score[loop]
repeat while score ˂ 0 or score ˃ 50
ask "You have not entered a score between 0 and 50. Please try again."
put it into score [loop]
end repeat
end repeat

Python – Version 3.x

for counter in range (5):
score[counter]= int(input ('Please enter a score between 0 and 50'))
while not (score[counter]˃=0 and score[counter]˂=50):
score [counter] = int(input ('You have not entered a score between 0 and 50. Please try again.'))

Java

for(int counter=0;counter˂5;counter++)
{
input=JOptionPane.showInputDialog("Please enter a score between 0 and 50");
score[counter]=Integer.parseInt(input);
while((score[counter]˂0)||(score[counter]˃50))
{
input=JOptionPane.showInputDialog("You have not entered a score between 0 and 50. Please try again ");
score [counter]=Integer.parseInt(input);
}
}

True BASIC

Remember - True BASIC will index from 1 rather than 0!

FOR counter = 1 To 5
INPUT PROMPT “Please enter a score between 0 and 50”: result
DO WHILE result ˂ 0 OR result ˃ 50
INPUT PROMPT “You have not entered a score between 0 and 50. Please try again.”: result
LOOP
LET score(counter) = result
NEXT counter

Xojo (Formerly REALbasic)

This example presumes that you have access to a pre-designed method for creating an InputBox.

For counter = 0 to 4
score(counter)=val(InputBox("Please enter a score between 0 and 50"))
While score(counter)˂0 OR score(counter)˃50
score(counter)=val(InputBox("Please re-enter your score. Your score must be between 0 and 50"))
Wend
Next

Pascal

begin
For counter := 0 to 4 do
begin
Writeln (‘Please enter a score between 0 and 50’)
Readln(result);
While result ˂ 0 or result ˃ 50 do
Begin
Writeln (‘Please re-enter your score. Your score must be between 0 and 50’)
Readln(result);
End;
score[counter] := result
End;
End.

Comal

FOR counter%:= 0 TO 4 DO
PRINT "Please enter a score between 0 and 50";
INPUT score%(counter%)
WHILE (score% ˂ 1) OR (score% ˃ 10) DO
PRINT “Please re-enter your score. Your score must be between 0 and 50";
INPUT score%(counter%)
ENDWHILE
NEXT