TO: Your Destination Setter (in FOR Loops and Commands)
Ready to chart a course for your Commodore 64 code? Look no further than the TO
keyword! While not a standalone command, it plays a crucial role in several BASIC constructs, acting as your destination setter, guiding loops and commands towards their final goal. Let's explore how TO
helps your code reach its intended destination!
TO in FOR Loops: Setting the Loop's End Point
The most common use of TO
is within FOR
loops. It defines the final value of your loop counter, determining how many times the loop will repeat.
FOR I = 1 TO 10
PRINT I;
NEXT I
In this example, TO 10
tells the loop to continue as long as the counter variable I
is less than or equal to 10.
TO in Commands: Specifying Ranges and Limits
The TO
keyword also pops up in other commands to set ranges or limits:
- LIST:
LIST 10-50
displays lines 10 through 50. - DELETE:
DELETE 20-30
removes lines 20 through 30. - SOUND:
SOUND 1,100,10
plays a sound on voice 1 with a pitch of 100 for 10 time units. Here,10
is the duration usingTO
.
Code Examples
1. FOR Loop with TO:
10 FOR J = 5 TO 1 STEP -1 :rem Counting down from 5 to 1
20 PRINT J;
30 NEXT J
The STEP -1
combined with TO 1
makes this loop run in reverse.
TO in the Wild: The Graphic Artist's Line Tool
Imagine you're writing a program to draw lines on the Commodore 64 screen. You might use a FOR
loop with TO
to specify the starting and ending coordinates of the line, instructing the computer to draw pixels between those points.
While TO
may be a small word, its impact on your Commodore 64 code is significant! It provides the crucial direction for loops and commands, ensuring they reach their intended goals. Whether you're counting iterations, defining ranges, or specifying durations, TO
is your trusty companion in setting clear destinations for your code. So embrace the guidance of TO
and let it lead your programs to successful outcomes!