Last time we managed to complete a program that selected a random whole number between 1 and 50, ostensibly for the UK Lottery. So far so good, but as you'll almost certainly know, lottery balls come in groups of 6 and the next stage for our number selector is to pick more than the one single figure.
This was our program as we left it at the end of the last lesson:
After a moment's thought, you'd probably agree that a very
quick way of getting the program to pick six numbers would be to physically
copy the "PRINT" line six times, so that when run the program would print six
numbers one after the other.
Ten out of ten for lateral thinking, but zero for elegance! What if we needed the program to generate 100 numbers? Or a thousand? The key to repeating tasks within a program is not usually to just duplicate the code itself but to use a 'loop'. In other words, find a way of telling the program "Look, just do this line 6 times".
In OPL on the Psion, loops are best done with the WHILE command. Let
me illustrate by writing out the logic needed:
Set a counter to 1
While the counter is less than, or equal to 6
Go back to the 'While' line and test the condition again
Please take a moment to understand how the loop above works, as the concept is absolutely fundamental to OPL programming.
The first step is to make a counter. We'll call it i%. You'll
quickly ask why, of course! A word of explanation is needed. All computer
programs need these counters, and you'll usually hear them referred to as
'variables'. This latter word implies that we can store other types of
information in them apart from whole numbers, and indeed you'll commonly run
into three types:
Thus i% is an integer, myvalue is a floating point and
name$ is a string variable. Once you get used to this naming
convention, things should start to become much clearer. And why did I pick the
letter "i" for our counter? Only because it's a common letter used for trivial
variables, such as counters, and if you run into i% (or j% for that matter) in
someone else's program, you can be fairly sure it's a counter of some kind.
(Actually, using "i" has its roots in the darkest days of FORTRAN, a 1970s
programming language, but I digress....)
Insert the following line before the "RANDOMIZE" one:
LOCAL i%
Your screen should now look like this:
Don't worry too much about the word "LOCAL" for now, it
simply means that the variable is one which works only in this particular
program. Let's carry on implementing the loop outlined earlier. Here are a few
more lines for you to type in. After the "RANDOMIZE" line, insert:
i%=1
WHILE i%<=6
And after the "PRINT" line, add:
i%=i%+1
ENDWH
That's a lot of changes, so please use the screen shot below to check your typing. Try to make your lines of code line up as neatly as mine by adding in spaces and tabs where necessary. They won't change the way the program works but will certainly help you understand it!
Can you see the way we've implemented the loop we talked
about earlier? The counter starts off with value 1 and then each time round the
WHILE....ENDWH loop it gets added to by another 1. Eventually, after six times
round the loop, the counter has the value 7 and thus the condition after the
"WHILE" statement is finally not true anymore. With its condition not met, the
program 'falls out' of the WHILE loop to the next statement in the program, in
this case the "GET".
When you've stared at the code on screen long enough to think you understand how it works, try translating and running it as usual. All being well, you should see something like the following display on your Psion screen:
And we thus have our six random numbers. We're not quite
there yet, though, as it's entirely possible that some of the numbers might be
the same. Which is OK from the numbers' point of view, but is pretty useless as
a set of lottery selections! Press a key to get back to the main code screen.
Let's take a break from entering code to consider another fundamental of programming: adding 'comments', plain english text lines added next to the lines of OPL code. There are two extremes in the world of OPL programming. On the one hand, some people love to add comments to every single line of code, with another 40 or 50 lines at the top of each section detailing exactly what the section does and doesn't do. On the other, some (rather lazy) programmers never use comments at all!
The former approach is usually over-the-top for would-be Psion programmers, adding to the file sizes and taking ages to write. The latter is fine for the first day or so, but without comments you'll find you won't be able to understand your own code a month down the road. As ever, a balance must be struck and I'd recommend that you just comment lines of code which you think warrant it. Basically, any line which you think you might not understand if you came back to it later on should be commented.
For example, let's add a comment to the first line of the program:
LOCAL i% rem Counter used in number selection loop
The "rem " bit stands for "Remark" and the Psion ignores any text it finds after it. You don't even have to have any actual code on a line, the remark/comment might be the sole entry. Blank lines are also allowed in OPL programs, so you can see that it's possible to space out and comment your program code as clearly as required. Put in blank lines before and after the "RANDOMIZE" line and then add a comment to the "PRINT" line as well:
PRINT INT(RND*50)+1 rem add 1 to take the number into the 1 to 50 range
Following the addition of the blank lines and comments, your screen should now look something like this:
The next step will be to add some titles and instructions to
our program, as well as the crucial step of making the program automatically
avoid duplicating numbers already 'drawn'. See you next week?
Go to next lesson | Programming index