Lesson 2: Conditional Loops

Imagine being on a hamster wheel that is closed off on every side. Since you are on a hamster wheel, you never move forward and you never move backwards. You can run forever, but you would not have moved an inch. You can never exit, no matter which way you turn. This is exactly like being stuck in an infinite loop. As fun as going around and around is, there will come a time when you just want to get out. At this time, you might ask yourself why you would want to repeat instructions forever! The answer is quite simple in most cases, you don't. This is brings us to the second category of loops — conditional loops.

This is where our good friend, “exit” comes in. The “exit” keyword instructs the computer to exit out of a loop. This keyword may not be interchanged with the “return” procedure. The “exit” keyword simply exits the loop containing it while “return” terminates the execution immidiately. When “exit” is the only token in the executable statement, it must be preceded by a selection statement such as an if statement or a case statement to avoid errors and be fully functional. For instance, consider the following program:

% The "Sum" program 
% Prompts the reader to perform an arithmetic calculation 
var sum : int
loop
	put "1 + 1 = " .. 
	get sum 
	if sum = 2 then
		exit 
	end if
end loop
put "Good job!"
			

The above program infinitely repeats the mathematical equation unless the user enters the correct sum, which is 2. The program may seem fully functional, however, it does not employ good programming etiquette. Instead of 'exit' inside an if statement, the keyword phrase 'exit when' may be used. This will be discussed in the coming paragraph. In any case, the program will exit and continue with the extro. But what would happen if we replaced 'exit' with 'return'? Why?

As we learn about repetition and utilizing the keyword 'loop' and the keyword phrase 'end loop', we also have to learn this mandatory aspect of loops, which is exiting with on a condition. Programmers sometimes use multiple loop statements in one program, and in order to get to the subsequent loop statements, we have to exit the first loop. The executable statement used for exiting a loop statement on a condition is 'exit when' followed by a condition.

Below is an example of an exit for a loop statement:

% The "Integer" Program 
% Outputs the sum of two user-supplied integers 
var sum, integer1, integer2: int
put "Integer 1: " ..
get integer1
put "Integer 2: " ..
get integer2 
loop
	sum := integer1 + integer2 
	put "Sum + 1 = ", sum 
	integer1 := integer1 + 1 
	exit when sum = 10 
end loop
				

The “exit when” keyword phrase is simple to impliment in a program as you simply type it in, and then place a boolean expression on its right. When the boolean value holds true, the loop body is not executed and the rest of the program code is executed. The process is fairly simple but as soon as you master it, you can create long interesting codes with loop statements within loop statements to create more extravagant codes.

The following are some possible options for a program which initializes the variable “number” and needs to exit the loop on the following conditions:

Remember that the sentinal value is because it guards against the next set of instructions! The values on the right of the number variable is the sentenal value. They are used for things such as:

Note that the sentinel values in the following program are “0” and “1”:

% The "Favourite Binary Digit" program 
% Prompts the user to select their favourite binary digit
loop
	put "Favourite binary digit [0 or 1]: " ..
	get favouriteDigit
	exit when favouriteDigit = "0" or favouriteDigit = "1" 
	% If the loop doesn't exit, it means the input is invalid.
	% Display an error message. 
	put "Error, your chose is not valid. Please only enter 1 or 2" 
	put skip
end loop
			

There are certain things that one must always consider when writing an exit statement. One of them is the placement of the statement in the loop body. This is extremely important, since having it too early or too late in the code, would provide an unwanted result. Another key thing to remember is to try and use a pre–declared sentinel value when possible. This would avoid the use of magic values, which even our own mentor has given explicit instructions to always avoid. Remember that there are very few situations in which you would have a loop without including an exit statement. An example of one of these situations would be when making a clock (in Turing, of course)! Now that you know to exit, when to exit, and where to exit, you should now be ready to start making your own loops.

Valid XHTML 1.0 Strict Valid CSS! Valid CSS!