Small Basic: Using a Sentinel for Loop Control


"The Sentinel" sounds like a newspaper. 

Or perhaps an action movie.

Or like a big robot that chases mutants.

But it's not! Well, not in this case. In this case, it's a way you can end or limit a loop. Let's take a look!

  

Let’s write a program that finds the average value of a set of N scores entered by your user, where N can be any value. You can start by asking your user how many scores he has, and then use a For loop to read his inputs. Listing 1 shows you how to do this.

Listing 1: Find the average of scores

1 ' Average1.sb

2 TextWindow.Write("How many scores do you have? ")

3 count = TextWindow.ReadNumber()

4 sum = 0

5 For N = 1 To count

6   TextWindow.Write("Enter score " + N + ": ")

7   sum = sum + TextWindow.ReadNumber()

8 EndFor

9 TextWindow.WriteLine("Average = " + (sum/count) )

     

Your program asks your user how many scores he has (Line 2), saves the answer in the count variable (Line 3), and then starts a For loop with a terminal value set to count. Each time, the loop asks him for a new score that gets added to the running total, sum (Lines 5-8). When the loop ends, the program calculates the average and displays it (Line 9).

    

Here’s an example of what it might look like to your user:

How many scores do you have? 4

Enter score 1: 90

Enter score 2: 85

Enter score 3: 95

Enter score 4: 88

Average = 89.5

   

Another way to write this program is to have your user enter a sentinel value (a special flag) when he’s done. (For example, you might ask your user to enter “-99” when he’s finished.) Of course, you must choose the sentinel so that it can’t be confused with the data. For example, if your program expects a list of names or positive numbers, a sentinel of –1 is a good choice. But if it expects the user to enter negative values, then a sentinel of –1 isn’t a good idea.

Back to our example! Let’s work through our averaging program using a sentinel of –1. The complete program’s in Listing 2.

Listing 2: Finds the average of a variable number of scores

 1 ' Average2.sb

 2 sum   = 0

 3 num   = 0

 4 count = 0

 5 While (num <> -1 )

 6   TextWindow.Write("Score " + (count+1) + " [-1 when done]: ")

 7   num = TextWindow.ReadNumber()

 8   If (num <> -1) Then

 9     sum = sum + num

10     count = count + 1

11   EndIf

12 EndWhile

13 TextWindow.WriteLine("Average = " + (sum/count))

  

Since the number of scores isn’t known in advance, you’ll want to use a While loop. Each time the loop prompts your user to enter a number (Line 6). Note that the value of the sentinel (–1 in this case) is stated in the prompt. The value entered by your user is then compared to the sentinel value (Line 8). If she enters –1, the While loop terminates, passing the control out to Line 13. Otherwise, her input value’s added to the running total (Line 9), and the loop runs again to prompt her for her next entry.

 

Here’s an example where she enters four numbers before entering the sentinel value:

Score 1 [-1 when done]: 90

Score 2 [-1 when done]: 85

Score 3 [-1 when done]: 95

Score 4 [-1 when done]: 88

Score 5 [-1 when done]: -1

Average = 89.5

   

Do you have any questions? Ask us! We're full of answers and other fine things!

Head to the Small Basic forum to get the most answers to your questions: 

http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/

       

   

And go to http://social.technet.microsoft.com/wiki/contents/articles/30513.download-microsoft-small-basic.aspx to download Small Basic and learn all about it!

    

Small and Basically yours,

   - Ninja Ed & Majed Marji

 

Skip to main content