“MSX BASIC” COLUMN by Orazio Cacciola (part 8)
The IF/THEN/ELSE Function
Greetings to everyone. Here we are again to continue the explanation of the various commands and functions of our Basic MSX.
As I wrote in the title, today I will talk about one of the most important functions of our well-loved MSX: IF/THEN/ELSE. It's fundamental to our programs and it's like cheese on macaroni (laughter). Surely anyone who has had a magazine with listings at hand will have noticed that it is always present. There are very few cases where it is not used.
IF/THEN/ELSE is used when our MSX needs to decide the path to take on two or more options. We can say that it is a conditional function.
It can be simple or complex, that is, the more topics it has to control, the more complex it becomes.
I'll give you a very simple example to help you understand how to use it:
10 SCREEN 0:CLS: A=0 20 A=A+1: PRINT A 30 IF A=10 THEN PRINT "Counting finished":END ELSE 20
On line 10, in addition to having set the screen as text and deleted everything with CLS (it is a command that clears the screen), I initialized the variable A.
On line 20 I add 1 to the variable and print it to the screen.
On 30, I check whether the value of variable A is equal to 10. If this is true, it executes the THEN, printing the phrase "Counting finished" and closes the program with END (Command used to terminate the program). If it is false, it executes the ELSE instruction, which sends the execution to the previous line, in short, it will repeat the cycle until the condition is true. It is not an obligation to use the ELSE, it can also be omitted, in fact we will use it when, in our IF/THEN line, we necessarily have to execute one of the 2 solutions.
Syntax: IF THEN expression [ELSE ]
Expression can be either a numeric or alphanumeric variable, or a line number to send the program back to. It can also be the calculation of 2 or more values, the checking of 2 strings, even with logical and relational expressions.
Let's remember that the comparisons must be variables or constants of the same type, I cannot do a check between a numeric variable and the other string, the MSX would report an error message.
Let's take another example with a more complex format, where I will use almost all the arguments I mentioned above.
We want our MSX, by entering a number from the keyboard, to tell us how to catalog it. Below I will explain line by line.
10 SCREEN 0: CLS 20 INPUT "Enter a number,(Max 500)";N$ 30 N=VAL(N$) 40 IF N$<"0" OR N$>"9" OR N>500 THEN 20 50 IF N MOD 2 =0 THEN PRINT "It is an even number" ELSE PRINT "It is an odd number" 60 IF N<10 THEN PRINT "It is a number within units" ELSE IF N>9 AND N< 100 THEN PRINT "Number which is in the tens" ELSE IF N>99 then PRINT "The number is in the hundreds" 70 IF (N>99 AND N<150) OR (N>199 AND N<250) OR (N>299 AND N<349) OR (N>399 AND N<450) THEN PRINT "The number falls within the first half of the hundreds" ELSE IF N>=0 AND N<50 PRINT "The number falls within the first half of the tens" ELSE "Second half" 80 INPUT "Do you want to continue (Y/N)";R$ 90 IF R$="S" OR R$="s" THEN 10 ELSE IF R$="N" OR R$="n" THEN END ELSE 80
Line 10 you already know what it does. The 20 waits for a keyboard entry with the INPUT function (I haven't talked about this yet, but I will in the next episodes).
30, creates the numeric variable N from N$, using the VAL function. The 40 makes 3 checks, for the continuation of the program they must all be false otherwise it will return to line 20 asking to enter the number again.
In the first and second, check the first character of the N$ variable that is not a number (the check is done with the ASCII code of the character, this will be explained later) and the third that it is greater than 500.
The 50 controls the rest of N with the MOD function, in fact if this is 0 it prints the first one if not the one after the ELSE.
The 60 controls where to catalog the number; units, tens or hundreds. In fact it will check with the relation operators, printing the true option.
The 70 is more complex. As you can see I used round brackets. These are valid as mathematical operations, that is, they have priority over everything. Therefore they will come
performed first. It will first check all the ANDs (since they are inside the brackets), if one of these is true, since the second check is with OR, it will print the wording after the PRINT. However, if all the ANDs are false it will move on to the second IF which will check whether N is within the specified limits, if this is also false it will print the last ELSE. As you have seen, I put multiple IFs in one program line as I did in the previous line, this is doable.
You can put as many IFs as you want as long as you don't exceed 255 characters per line.
Let's remember that after the IF there is always the ELSE and if one of the 2 is true, it will skip directly to the next line avoiding checking everything else.
Line 80, another headboard entry. This time the request is specific, just 2 characters.
The 90 checks whether the required characters have been entered. The check is done on uppercase and lowercase letters. If one of the 2 turns out to be true, it will go
to the specified program line or it will end the program otherwise if we have typed something different, it will repeat the question.
There are many other uses for using IF/THEN, but these will be learned when you have mastered the language, indeed you will be the ones to create increasingly complex algorithms for your long programs.
Thanks to everyone and see you next time



0 Comments