“MSX BASIC” COLUMN by Orazio Cacciola (part 10)
FOR/NEXT, READ, DATA, RESTORE
Here we are again explaining new functions of our well-loved MSX Basic.
In this episode I will talk about a group of functions that play a fundamental role in our programs. It is better to get to know them at the same time, because they almost always work together, that is, they need each other.
FOR/NEXT
Do you remember that in one of the previous episodes, in one of my little programs, I inserted a count that ended with the occurrence of a condition (IF/THEN/ELSE lesson). This function does just that, it unifies all those instructions into a function or rather a group, but as if they were just one because they necessarily have to "work" together. It executes a loop with a beginning and an end, only when this is finished will it jump out. It can perform a counting of increasing or decreasing numbers and also with different steps: FOR I=1 TO 10: PRINT I: NEXT I. The FOR is the starting point of the variable I which starts from 1 and ends (TO) at 10. Prints the I 10 times, incrementing by 1. NEXT is the end, checks the variable if it has reached 10, otherwise it will postpone the execution to FOR. As I said above, we can change the step of the increment or decrement to our liking using another piece of the group: STEP: FOR I=1 TO 10 STEP 2: PRINT I: NEXT I. This time it will print the variable 5 times counting 2 numbers at a time. We can do it the other way around: FOR I=10 TO 1 STEP-1: PRINT I: NEXT I. If we want to do as above, i.e. decrease 2 numbers at a time, just change; STEP-1 to STEP-2. I must point out that while for increasing numbers if we use the increment of a number it is not necessary to use STEP (it does so by default), for decreasing ones yes, otherwise the program will remain stuck in an endless loop. If the program encounters another FOR without having read a NEXT it will keep it in memory but if it encounters 2 NEXTs without FOR it will report the error. You can use loops within other loops, so-called nested loops. Below I will give an example with a miniprogram of a simple nested loop, with only 2 FOR/NEXT functions:
10 FOR I=0 TO 9 20 FOR J=0 TO .9 STEP .1 30 PRINT I;J;" "; 40 NEXT J: PRINT: NEXT I 50 PRINT I
It will print a counter from 1 to 10 using decimals. For each row 10 numbers: 1.0 1.1 1.2 up to 1.9 and then in the next row it will start again with 2.0 etc. The loop will end at 9.9, but if you try the program you will realize that the PRINT of line 50 will print the number 10. Why does this happen given that the variable I in the loop ends with 9? It's easy to say. It is NEXT that adds the increment and when it reaches the last count, in our case 9, it will jump to the next instruction, counting another increment. Therefore, if you have to use the loop variable in the continuation of our program, remember that it will be greater or less, depending on the case, than the result at the end of the cycle.
Another important thing; When using nested loops, remember to always close the last one you created first.
Syntax: FOR Variable = Start number TO End number [STEP Number]:
Instructions: NEXT [Variable]. The numbers can also be variable.
READ
Syntax: READ Variable, variable, ….
This function is like a ROM inside the Basic, The data inside it can only be read. This is permitted only and exclusively by the READ function. Each data must be separated by a comma. For all other commands and functions, it is treated as a comment line. It can contain numbers and strings. They can also be mixed, it all depends on how the reading function, READ, has been set.
Example:
10 DATA 1,MSX,3,NUMBER
20 READ A,A$,B,B$
30 PRINT A$;B;B$;A
Syntax: DATA Argument, Argument, ….
It is a command that resets the memory of the data read with READ and DATA. If used without arguments, it will clear everything and start from the first DATA line of the program,
if instead we want to have it reread from a certain point in our program, just add the line number, it will reset from that point onwards.
Syntax: RESTORE [Line Number]
To make everything I said better understood, I created a program to summarize all the functions explained in this lesson. Feel free to ask questions in the comments if you don't understand something. I am at your complete disposal.
10 REM ** Knowing prime numbers ** 10 SCREEN 0:CLS:DEFINT AZ 20 RESTORE 140:READ C$: READ E$: RESTORE 30 INPUT "Enter a prime number (MAX 200)";N 40 IF N<1 OR N >200 THEN 20 50 FOR I=1 TO 45 60 READ P: IF N=P THEN PRINT N; C$: END 70 NEXT I: PRINT N; E$ 80 INPUT "Do you want to try again";D$ 90 IF D$="s" OR D$="S" THEN RESTORE 110: GOTO 30 100 IF D$="N" OR D$="n" THEN CLS: END ELSE 80 110 DATA 2 ,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 120 DATA 101,103,107,109,113,127,131,137,139,149,151,157,163,173,179,181,191 130 DATA 193,197,199 140 DATA "IT'S A PRIME NUMBER","IT'S NOT A PRIME NUMBER"



0 Comments