# Thread: Algorithm

1. ## Algorithm

This is working algorithm version 1 when Exam is of type Integer
Code:
```// program exits loop when Exam is valid or
// when TERMINATION_VALUE = -99 is entered
start loop
output to screen "Enter a score"
store input to Exam
exit loop when ( 0 <= Exam <= 100 ) or
when ( Exam = TERMINATION_VALUE)
output "exam score is 0 .. 100"
end loop```
Second version with Exam is of type Integer 1 .. 100
Code:
```// program exits loop when Exam is valid or
// when TERMINATION_VALUE = -99is entered
start loop
begin handler block
output to screen "Enter a score"
store input to Exam
exit
exception when CONSTRAINT_ERROR
output "exam score is 0 .. 100"

end handler block
end loop```
Second version exits loop when Exam is valid, but then how to exit when Exam = TERMINATION_VALUE

2. Use a do while() loop.

3. Originally posted by cyberCLoWn
Use a do while() loop.
Not really the answer that I expected. My code could be implemented as do-while loop but when Exam = TERMINATION_VALUE, the CONSTRAINT_ERROR exception catches it (instead of breaking out of the loop) and it's back to looping again

4. ## Re: Algorithm

Originally posted by alphaoide
Second version with Exam is of type Integer 1 .. 100
Code:
```// program exits loop when Exam is valid or
// when TERMINATION_VALUE = -99is entered
start loop
begin handler block
output to screen "Enter a score"
store input to Exam
exit
exception when CONSTRAINT_ERROR
output "exam score is 0 .. 100"

end handler block
end loop```
Second version exits loop when Exam is valid, but then how to exit when Exam = TERMINATION_VALUE
By your definition,
1) exam can be only 0 -> 100 (or 1-> 100 depending on which information you actually code)
2) TERMINATION_VALUE = -99
since TERMINATION_VALUE is an illegal value, the exception is thrown.

Try changing to:
1) exam can be only -1 -> 100
2) TERMINATION_VALUE = -1

Popular pages Recent additions