4.3 More Complex Conditional Loops

Conditonal loops are often used for error checking. Suppose we want the ensure that the user enters input that we are expecting. Consider a fragment that forces the user to enter a number between 1 and 10 (inclusive).

var num : int
loop
   put "Enter a number between 1 and 10"
   get num
   exit when num >= 1 and num <= 10
   put "Invalid entry.  Please try again"
end loop
 

Here is an the output if the user enters 11, -3 and then 7:

Enter a number between 1 and 10
11
Invalid entry.  Please try again
Enter a number between 1 and 10
-3
Invalid entry.  Please try again
Enter a number between 1 and 10
7

In the previous example we'll still be in trouble if the user types something other than an int. It would be nice if we could prevent the program from crashing if that happens. We'll read the input as a string. Turing has functions that can check if the input is a valid int or real without crashing. There are also functions to convert the string to an int or a real.

strint

The STRing to INT function takes a string as a parameter and converts it into an integer, if possible. There can be blanks in the front of the string but then only plus, minus or digits are allowed. If there are other characters then the strint function causes an error.

strintok

The STRing to INT is it OK function takes a string as a parameter returns a boolean value that indicates if it would be OK to convert that value to an int.

There are also also functions called strreal and strrealok that do the same thing except convert and check for real numbers instead of ints.

Here is an example program that get a user to enter a valid int. It will not crash regardless of what they type and will make them keep trying until they enter a valid value:

var strNum : string
var num : int

loop
   put "Enter an integer"
   get strNum:*
   exit when strintok(strNum)
   put "You've entered characters that are not allowed in an integer"
   put "Please try again"
end loop

num := strint(strNum)
put "You entered ", num 

This program reads a number as a string. It uses strintok to determine if the string can be safely converted to an int. If it can the loop exits. If not the user is told what they did wrong and given another chance. Here is a sample output:

Enter an integer
123abc
You've entered characters that are not allowed in an integer
Please try again
Enter an integer
123.43
You've entered characters that are not allowed in an integer
Please try again
Enter an integer
$3
You've entered characters that are not allowed in an integer
Please try again
Enter an integer
8
You entered 8

Exercise 4.3

  1. Describe the output briefly and then rewrite each fragment using a for loop.
    1. var count : int := 1
      
      loop
         exit when count >= 10
         for i : 1 .. count
         	put " "..
         end for
         put "*"
         count := count + 1
      end loop 
    2. var i : int := 5
      
      loop
         i := i - 1
         put i**2
         exit when i = 1
      end loop 
  2. Write a program that asks the user for a password (defined by a constant within the program). The program should then test the validity of the password. If it is correct, the user should be given a welcoming message. If it is incorrect after three tries, the computer should give some nasty message and then stop.
  3. Write a program that requires the user to provide, as input, a positive int less than 1000. Prompt the user to enter a number and then reject invalid integers repeatedly until an appropriate response has been made.