-
Notifications
You must be signed in to change notification settings - Fork 51
/
beginninginbasic.tex
1523 lines (1236 loc) · 71.6 KB
/
beginninginbasic.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Getting Started in BASIC}
\label{cha:basic-getting-started}
It is possible to code on the MEGA65 in many languages,
however most people start with BASIC. That makes sense,
because BASIC stands for Beginner's All-purpose Symbolic
Instruction Code: It was made for people like you to get
started with in the world of coding!
A few short words before we dive in: BASIC is a programming
language, and like spoken language it has conventions, grammar
and vocabulary. Fortunately, it is much quicker and easier
to learn than our complex human languages. But if you pay
attention, you might notice some of these structures, and that
can help you along your path in the world of coding.
If you haven't already read \bookvref{cha:getting-started},
it might be a good idea to do so. This will help you be able to
more confidently interact with the MEGA65 computer.
It's also great to remember that if you really confuse the MEGA65,
you can always get back to the READY. prompt by just pressing the
reset button on the left-hand side of the keyboard, or if that
doesn't help, then by turning it off
and on again using the power switch on the left-hand side of the keyboard.
You don't have to worry about shutting the computer
down properly or any of that nonsense. The only thing to remember
is that if you had any unsaved work, it will be lost when you switch
the computer off and on again or press the reset button.
Finally, if you don't understand all of the descriptions and information
with an example -- don't worry! We have provided as much information
as we can, so that it is there in case you have questions, encounter problems are
just curious to discover more. Feel free to skip ahead to the examples
and try things out, and then you can go back and re-read it when you are motivated
to find something out, or help you work though a problem. And if you don't find
the answer to your problem, send us a message! There are support forums for the
MEGA65 at \url{https://mega65.net}, and you can
report problems with this guide at:
\url{https://github.com/mega65/mega65-user-guide}
We hope you have as much fun learning to program the MEGA65 as
we have had making it!
\section{Your first BASIC programs}
The MEGA65 was designed to be programmed! When you switch it on,
it takes a couple of seconds to get its house in order, and then
it quickly shows you a ``READY.'' prompt and flashing block called
the cursor. When the cursor is blinking, it tells you that the
computer is waiting for input. The ``READY.'' message tells you
that the BASIC programming language is running and ready for you to
start programming. You don't even need to load any programs --
you can just get started.
\needspace{4cm} % Dont allow following paragraph to separate from
% following element
Try typing the following into the computer and see what happens:
\begin{screencode}
HELLO COMPUTER
\end{screencode}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
To do this, just type the letters as you see them above. The computer
will already be in uppercase mode, so you don't need to hold \specialkey{SHIFT}
or \specialkey{CAPS\\LOCK} down. When you have typed "HELLO COMPUTER", press
\specialkey{RETURN}. This tells the computer you want it to accept the
line of input you have typed. When you do this, you should see a message something
like the following:
\screenshotwrap{images/getting-started/syntax-error.png}
If you saw a \screentextwide{SYNTAX ERROR} message something like that one, then congratulations:
You have succeeded in communicating with the computer!\index{Errors!Syntax}\index{SYNTAX ERROR}
Error messages sound much nastier than they are. The MEGA65 uses them, especially
the syntax error to tell you when it is having trouble understanding what you have
typed, or what you have put in a program. They are nothing to be afraid of, and
experienced programmers get them all the time.
In this case, the computer was confused because it doesn't understand the word
``hello'' or the word ``computer''. That is, it didn't know what you wanted it to
do. In this regard, computers are quite stupid. They know only a few words, and
aren't particularly imaginative about how they interpret them.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
So let's try that again in a way that the computer will understand. Try typing
the following in. You can just type it right away. It doesn't matter that the
syntax error message can still be seen on the screen. The computer has already
forgotten about that by the time it told you \screentextwide{READY.} again.
\begin{screencode}
PRINT "HELLO COMPUTER"
\end{screencode}
Again, make sure you don't use shift or shift-lock while typing it in. The symbols around
the words \screentextwide{HELLO COMPUTER} are double-quotes. If you are used to an Australian or American
keyboard, you might discover that they double-quote key is in a rather different place to
where you are used to: Double-quotes can be typed on the MEGA65 by holding down
\specialkey{SHIFT}, and then pressing \megakey{2}. Don't forget to press \specialkey{RETURN}
when you are done, so that the computer knows you want it to do something with your input.
If you make a mistake while typing, you can use \specialkey{INST\\DEL} to rub out the mistake
and fix it up. You can also use the cursor keys to move back and forth on the line while
you edit the line you are typing, but there is a bit of a trick if you have already typed
a double-quote: If you try to use the cursor keys, it will print a funny reversed symbol
instead of moving the cursor. This is because the computer thinks you want to record
moving the cursor in the text itself, which can be really useful and fun, and which you can
read more about in \bookvref{cha:getting-started}. But for now, if you
make a mistake just press \specialkey{RETURN} and type the messed up line again.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
Hopefully now you will see something like the following:
\screenshotwrap{images/getting-started/print-hello-computer.png}
This time no new \screentextwide{SYNTAX ERROR} message should appear. But if some kind
of error message has appeared, just try typing in the command again, after
taking a close look to work out where the mistake might be.
Instead of an error, we should see \screentextwide{HELLO COMPUTER} repeated underneath
the line you typed in. The reason this happened is that the computer
does understand the word \screentextwide{PRINT}. It knows that whatever comes after
the word \screentextwide{PRINT} should be printed to the screen. We had to put \screentextwide{HELLO
COMPUTER} inside double-quotes to tell the computer that we want it to be
printed literally.
If we hadn't put the double-quotes in, the computer would have thought
that \screentextwide{HELLO COMPUTER} was the name of a stored piece of information.
But because we haven't stored any piece of information in such a place,
the computer will have zero there, so the computer will print the number
zero. If the computer prints zero or some other number when
you expected a message of some sort, this can be the reason.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
You can try it, if you like, and you should see something like the following:
\screenshotwrap{images/getting-started/print-hello-computer-no-quotes.png}
In the above examples we typed commands in directly, and the computer executed
them immediately after you pressed \specialkey{RETURN}. This is why
typing commands in this way is often called {\em direct mode} (sometimes called ``immediate mode'').
But we can also tell the computer to remember a list of commands to execute one
after the other. This is done using the rather unimaginatively named {\em non-direct mode}.
To use non-direct mode, we just put a number between 0 and 63999 at the start of
the command. The computer will then remember that command. Unlike when we executed
a direct-mode command, the computer doesn't print \screentextwide{READY.} again. Instead the cursor
just reappears on the next line, ready for us to type in more commands.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
Let's try that out with a simple little program. Type in the following three lines of
input:
\begin{screencode}
1 FOR I = 1 TO 10 STEP 1
2 PRINT I
3 NEXT I
\end{screencode}
\index{FOR}
\index{BASIC 65 Commands!FOR}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
When you have done this, the screen should show something like this:
\screenshotwrap{images/getting-started/first-steps-for-loop-programme-1.png}
If it doesn't you
can try again. Don't forget, if you feel that the computer is getting all muddled up,
you can just press the reset button or flip the power switch off and on, at the left-hand side of the
computer to reboot it. This only takes a couple of seconds, and doesn't hurt the MEGA65
in anyway.
We have told the computer to remember three commands, that is, \screentextwide{FOR I = 1 TO 10 STEP 1},
\screentextwide{PRINT I}
and \screentextwide{NEXT I}. We have also told the computer which order we would like to run them in: The
computer will start with the command with the lowest number, and execute each command that
has the next higher number in turn, until it reaches the end of the list. So it's a bit like
a reminder list for the computer. This is what we call a program, a bit like the program at
a concert or the theatre, it tells us what is coming up, and in what order.
So let's tell the computer to execute this program.
But first, let's try to guess what will happen. Let's start with the middle command, \screentextwide{PRINT I}.
We've seen the \screentextwide{PRINT} command, and we know it tells the computer to print things to the screen.
The thing it will try to print is \screentextwide{I}. Just like before, because there are no double-quotes
around the \screentextwide{I}, it will try to print a piece of stored information. The piece of information
it will try to print will be the piece associated with the thing \screentextwide{I}.
When we give a piece of
information like this a name, we call it a {\em variable}\index{variable}. They are called
variables because they can vary. That is, we can replace the piece of information associated
with the variable called I with another piece of information. The old piece will be forgotten
as a result. So if we gave a command like \screentextwide{LET I = 3}, this would replace whatever was stored
in the variable called \screentextwide{I} with the number 3.
Back to our program, we now know that the 2\textsuperscript{nd} command will try to print the piece of information
stored in the variable \screentextwide{I}. So let's look at the first command: \screentextwide{FOR I = 1 TO 10 STEP 1}. Although
we haven't seen the \screentextwide{FOR} command before, we can take a bit of a guess at how it works. It looks like
it is going to put something into the variable \screentextwide{I}. That something seems to have something to do
with the range of number 1 through 10, and a step or interval of 1. What do you think it will do?
If you guessed
that it will put the values 1, 2, 3, 4, 5, 6, 7, 8, 9 and then 10 into the variable \screentextwide{I}, then you
can give yourself a pat on the back, because that's exactly what it does. It also helps us to
understand the 3\textsuperscript{rd} command, \screentextwide{NEXT I}: That command tells the computer to put the next value into
the variable \screentextwide{I}. And here is a little bit of magic: When the computer does that, it goes back
up the list of commands, and continues again from the command after the \screentextwide{FOR} command.
So let's pull that together: When the computer executes the first command, it discovers that it has
to put 10 different values into the variable \screentextwide{I}. It starts by putting the first value in there, which
in this case will be the number 1.
The computer then continues to the second command, which tells the computer to print the piece of
information that is currently stored in the variable called \screentextwide{I}. That will be the number 1, since
that was the last thing the computer was told to put there. Then the computer proceeds to the
third command, which tells it that it is time to put the next value into the variable \screentextwide{I}. So the
computer will throw away the number 1 that is currently in the variable \screentextwide{I}, and put the number 2 in
there, since that is the next number in the list. It will then continue from the 2\textsuperscript{nd} command,
which will cause the computer to print out the contents of the variable \screentextwide{I} again. Except that this
time \screentextwide{I} has had the number 2 stored in it most recently, so the computer will print the number 2.
This process will repeat, until the computer has printed all ten values that the \screentextwide{FOR} command
indicated it to do.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
To see this in action, we need to tell the computer to execute the program of commands we typed in.
We do this by using the \screentextwide{RUN} command. Because we want it to run the program immediately, we
should use direct mode.
So just type in the word \screentextwide{RUN} and press \specialkey{RETURN}. You should then see a display
that looks something like the following:
\screenshotwrap{images/getting-started/first-steps-for-loop-programme-1-running.png}
You might notice a couple of things here:
First, the computer has told us it is \screentextwide{READY.} again
as soon as it finished running the program. This just makes it easier for us to know when we
can start giving commands to the computer again.
Second, when the computer got to the bottom of the screen
it automatically scrolled the display up to make space. This is quite normal. What is important
to remember, is that the computer forgets everything that scrolls off the top. The only exception
is if you have told the computer to remember a command by putting a number in front of it. So
our program is quite safe for now. We can see that this is the case by typing the \screentextwide{RUN} command a
couple more times: The program listing will have scrolled off the top of the screen, but we can
still RUN the program, because the computer has remembered it. Give it a try!
Did it work?
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
If you wish to see the program of remembered commands, you can use the \screentextwide{LIST}\index{LIST}\index{BASIC 65 Commands!LIST}
command. This commands causes the computer to display the remembered program of commands to the screen, like in the display here.
If you would like to replace any of the commands in the program, you can type a new line that has the same number as the one you
wish to change.
\screenshotwrap{images/getting-started/first-steps-for-loop-programme-1-listing.png}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
For example, to print the results all on one line, we could modify the second line of the program to \screentextwide{PRINT I;} by
typing the following line of input and pressing \specialkey{RETURN}:
\begin{screencode}
2 PRINT I;
\end{screencode}
\index{PRINT}
\index{BASIC 65 Commands!PRINT}
%\end{tcolorbox}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
You can make sure that the change has been remembered by running the \screentextwide{LIST} command again, as we can see here.
You can then use the \screentextwide{RUN} command to run the modified
program, like this:
\screenshotwrap{images/getting-started/first-steps-for-loop-programme-1-modified.png}
It is quite easy to modify your programs in this way. As you become more comfortable with the process, there are two
additional helpful tricks:
First, you can give the {\bf LIST} command the number of a command, or line as they are referred to, and it will display only
that line of the program. Alternatively, you can give a range separated by a minus sign to display only a section of the program,
e.g., \screentextwide{LIST 1 - 2} to list the first two lines of our program.
Second, you can use the cursor keys to move the cursor to a line which has already been remembered and is displayed on the screen. If you
modify what you see on the screen, and then press \specialkey{RETURN} while the cursor is on that line, the BASIC interpreter will
read in the modified line and replace the old version of it. It is important to note that if you modify multiple lines of the program
at the same time, you must press \specialkey{RETURN} on each line that has been modified. It is good practice to check that the
program has been correctly modified. Use the {\bf LIST}\index{LIST}\index{BASIC 65 Commands!LIST} command again to achieve this.
\subsection{Exercises to try}
{\bf 1. Can you make it count to a higher or lower number?}
At the moment it counts from 1 to 10. Can you change it to count to 20 instead? Or to count from 3 to 17?
Or how about from 14.5 to 21.5? What do you think you would need to reverse the order in which it counts?
{\em Clue:} You will need to modify the \screentextwide{FOR} command.
{\bf 2. Can you change the counting step?}
At the moment it counts by ones, i.e., each number is one more than the last. Can you change it to count by twos
instead? Or by halves, so that it counts 1, 1.5, 2, 2.5, 3, \ldots?
{\em Clue:} You will need to modify the \screentextwide{STEP} clause
of the \screentextwide{FOR} command.\index{STEP}\index{BASIC 65 Commands!STEP}
{\bf 3. Can you make it print out one of the times tables?}
At the moment it prints the answers to the 1 times tables, because it counts by ones.
Can you make it count by threes, and show the three times tables?
{\em Clue:} You will need to modify the \screentextwide{FOR} command.
{\bf 4. Can you make it print out the times tables from 1$\times$1 to 10$\times$10?}
{\em Clue:} You might like to use ; on the end of \screentextwide{PRINT} command, so that you can have
more than one entry per line on the screen.\\
{\em Clue:} The \screentextwide{PRINT} command without any argument will just advance to the start of the next line.\\
{\em Clue:} You might need to have multiple \screentextwide{FOR} loops, one inside the other.
\section{First steps with text and numbers}
In the last section we started to use both numbers and text. Text on computers is made by stringing individual letters
and other symbols together. For this reason they are called {\em strings}. We also call the individual letters and
symbols {\em characters}. The name character comes from the printing industry where it refers to each symbol that can be
printed on a page. For computers, it has much the same meaning, and the set of characters that a computer can display
is rather unimaginatively called a {\em character set}.\index{character}\index{character set}\index{string}.
When the MEGA65 expects some form of input, it is typically looking for one of four things:
\begin{enumerate}
\item {\em a keyword} like \screentextwide{PRINT} or \screentextwide{STEP}, which are words that have a special meaning to the computer;
\item {\em a variable name} like \screentextwide{I} or \screentextwide{A\$} that it will then use to either store or retrieve a piece of information;
\item {\em a number} like \screentextwide{42} or \screentextwide{-30.3137}; or
\item {\em a string} like \screentextwide{"HELLO COMPUTER"} or \screentextwide{"23 KILOMETRES"}.
\end{enumerate}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
Sometimes you have a choice of which sort of thing you can provide, while other times you have less choice. What
sort of thing the computer will accept depends on what you are doing at the time. For example, in the previous
section we discovered that when the computer tells us that it is \screentextwide{READY}, that we can give it
a keyword or a number. Do you think that the computer will accept all four kinds of things when it says
\screentextwide{READY.}? We already know that keywords and numbers and keywords can be entered, but what about
variable names or strings? Let's try typing in a variable name, say \screentextwide{N}, and pressing \specialkey{RETURN},
and see what happens. And then let's try with a string, say \screentextwide{"THIS IS A STRING"}.
\screenshotwrap{images/getting-started/typing-variable-name-or-string}
You should get a syntax error each time, telling you that the computer doesn't understand the input you have given it.
Let's start with when you typed the variable: If you just tell the computer the name of a stored piece of information,
it doesn't have the foggiest idea what you are wanting it to do. It's the same when you give it a piece of information,
like a string, without telling the computer what to do with it.
But as we discovered in the last section, we can tell the computer that we want to see the piece of information stored
in a variable using the \screentextwide{PRINT} command. So instead, we could type in \screentextwide{PRINT N}, and
the computer would know what to do, and will print the piece of information stored in the variable called \screentextwide{N}.
In fact, using the \screentextwide{PRINT} command is so common, that programmers got annoyed having to type in the \screentextwide{PRINT}
command all the time, that they made a shortcut: If you type a question mark character, i.e., a \screentextwide{?}, the computer
knows that you mean \screentextwide{PRINT}. So for example if you type \screentextwide{? N}, it will do the same as typing
\screentextwide{PRINT N}. Of course, you have to press \specialkey{RETURN} after each command to tell the computer
you want it to process what you typed. From here on, we will assume that you remember to do that, without being reminded.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
The \screentextwide{?} shortcut also works if you are telling the computer to remember a command as part of a program.
So if you type \screentextwide{1 ? N}, and then \screentextwide{LIST}, you will see \screentextwide{1 PRINT N}, as we can see
in the following screenshot:
\screenshotwrap{images/getting-started/print-question-mark}
Like we saw in the last section, the variable \screentextwide{N} has not had a value stored in it, so when the computer looks for
what is there, it finds nothing. Because \screentextwide{N} is a {\em numeric variable}\index{variable!numeric}, when there is
nothing there, this means zero. If it was a {\em string variable}\index{variable!string}, then it would have found literally nothing.
We can try that, but first we have to explain how we tell the computer we are talking about a string variable. We do that by
putting a dollar sign character, i.e., a \screentextwide{\$}, on the end of the variable name. So if we put a \screentextwide{\$} on
the end of the variable name \screentextwide{N}, it will refer to a string variable called \screentextwide{N\$}.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
We can experiment with these variables by using the hopefully now familiar
\screentextwide{PRINT} command (or the \screentextwide{?} shortcut)
to see what is in the variables. But we need a convenient way to put
values into them. Fortunately we aren't the first people wanting to
put values into variables, and so the
\screentextwide{LET}\index{LET}\index{BASIC 65 Commands!LET} exists.
The \screentextwide{LET} command is used to put a value into a
variable. For example, we can tell the computer:
\begin{screencode}
LET N = 5.3
\end{screencode}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
This tells the computer to put the value 5.3 into the variable
\screentextwide{N}. We can then use the \screentextwide{PRINT}
command to check that it worked. Similarly, we can put a value into
the variable \screentextwide{N\$} with something like:
\begin{screencode}
LET N$ = "THE KING OF THE POTATO PEOPLE"
\end{screencode}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
If we try those, we will see something like the following:
\screenshotwrap{images/getting-started/let-command-examples}
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
We mentioned just before that \screentextwide{N} is a numeric
variable and that \screentextwide{N\$} is a string variable. This
means that we can only put numbers into \screentextwide{N} and
strings into \screentextwide{N\$}. If we try to put the wrong kind
of information into a variable, the computer will tell us that we have
mis-matched the kind of information with the place we are trying to
put it by giving us a \screentextwide{TYPE MISMATCH
ERROR}\index{Errors!Type mismatch}\index{Type mismatch error} like
this:
\screenshotwrap{images/getting-started/type-mismatch-errors}
This leads us to a rather important point: \screentextwide{N} and
\screentextwide{N\$} are separate variables, even though they have
similar names. This applies to all possible variable names: If the
variable name has a \screentextwide{\$} character on the end, it
means it is a string variable quite separate from the similarly named
numeric variable. To use a bit of jargon, this means that each {\em type}
of variable has their own separate {\em name
spaces}\index{name spaces}.
(There are also four other variable name
spaces that we haven't talked about yet: integer variables, identified
by having a \screentextwide{\%} character at the end of their name,
e.g., \screentextwide{N\%}, and arrays of numeric, string or integer
variables. But don't worry about those for now.
We'll talk about those a bit later on.)
So far we have only given values to variables in direct mode, or
by using constructions like \screentextwide{FOR} loops. But we
haven't seen how we can get information from the user when a program
is running. One way that we can do this, is with the
\screentextwide{INPUT}\index{BASIC 65 Commands!INPUT}\index{INPUT}
command.
\needspace{4cm} % Dont allow following paragraph to separate from
% following screenshot
\screentextwide{INPUT} is quite easy to use: We just have to say which
variable we would like the input to go into. For example, to tell the
computer to ask for the user to provide something to put into the
variable \screentextwide{A\$}, we could use something like
\screentextwide{INPUT A\$}. The only trick with the \stw{INPUT}
command is that it cannot be used in direct mode\index{Direct Mode}.
If you try it, the computer will tell you \stw{ILLEGAL DIRECT
ERROR}\index{Errors!Illegal Direct}\index{Illegal Direct Error}.
Try it, and you should see something like the following
\screenshotwrap{images/getting-started/illegal-direct-error}
\needspace{4cm}
This means that the \stw{INPUT} command can only be used as part of a
program. So we can instead do something like the following:
\begin{screencode}
1 INPUT A$
2 PRINT "YOU TYPED "; A$
RUN
\end{screencode}
\needspace{4cm}
What do you think that this will do? The first line will ask the
computer for something to put into the variable \stw{A\$}, and the
second line will print the string \stw{"YOU TYPED"}, followed by
what the \stw{INPUT} command read from the user. Let's try it out:
\screenshotwrap{images/getting-started/input-example-1}
Did you expect that to happen? What is this question mark doing there?
The \stw{?} here is the computer's way of telling you that a
program is waiting for some input from you. This means that the
computer uses the same symbol, \stw{?}, to mean two different things:
If you type it as part of a program or in direct mode, then it is a
short-cut for the \stw{PRINT} command. That's when you type it. But if
the computer shows it to you, it has this other meaning, that the
computer is waiting for you to type something in. There is also a
third way that the computer uses the \stw{?} character. Have you
noticed what it is? It is to indicate the start of an error
message. For example, a Syntax Error is indicated by \stw{?SYNTAX
ERROR}. When a character or something has different meanings in
different situations or contexts, we say that it its {\em context
dependent}\index{context dependent}.
\needspace{4cm}
But returning to our example, if we now type
something in, and press \specialkey{RETURN} to tell the
computer that you are done, the program will continue, like this:
\screenshotwrap{images/getting-started/input-example-2}
\needspace{4cm}
Of course, we didn't really know what to type in, because the program
didn't give any hints to the user as to what the programmer wanted
them to do. So we should try to provide some instructions. For
example, if we wanted the user to type their name, we could print a
message asking them to type their name, like this:
\begin{screencode}
1 PRINT "WHAT IS YOUR NAME"
2 INPUT A$
3 PRINT "HELLO "; A$
\end{screencode}
\needspace{4cm}
Now if we run this program, the user will get a clue as to what we
expect them to do, and the whole experience will make a lot more sense
for them:
\screenshotwrap{images/getting-started/input-example-3}
When we run the program, we first see the \stw{WHAT IS YOUR NAME} message
from line 1. The computer doesn't print the double-quote symbols,
because they only told the computer that the piece of information
between them is a string. The string itself is only the part in
between.
After this we see the \stw{?} character again and the blinking cursor
telling us that the computer is waiting for some input from us. The
rest of the programmed is {\em blocked}\index{I/O!blocking}\index{blocked} from continuing until it we type the
piece of information. Once we type the piece of input, the computer
stores it into the variable \stw{A\$}, and can continue. Thus when it
reaches line 3 of the program, it has everything it needs, and
prints out both the \stw{HELLO} message, as well as the information
stored in the variable called \stw{A\$}.
Notice that the word \stw{LISTER} doesn't appear anywhere in the
program. It exists only in the variable. This ability to process
information that is not part of a program is one of the things that
makes computer programs so powerful and able to be used for so many
purposes. All we have to do is to change the input, and we can get
different output.
\needspace{4cm}
For example, with our program we run it again and again, and give it
different input each time, and the
program will adapt its output to what we type. Pretty nifty, right?
Let's have the rest of the crew try it out:
\screenshotwrap{images/getting-started/input-extra-ignored-1}
We can see that each time the program prints out the message
customised with the input that you typed in\ldots Until we get to
\stw{RIMMER, BSC}. As always, Mr. Rimmer is causing trouble. In this
case, he couldn't resist putting his Bronze Swimming Certificate
qualification on the end of his name.
We see that the computer has
given us a kind of error message, \stw{?EXTRA IGNORED}\index{Extra
Ignored}\index{Errors!Extra Ignored}\index{Warnings!Extra Ignored}.
The error is not written in red, and doesn't have the word \stw{ERROR}
on the end. This means that it is a warning, rather than an error.
Because it is only a warning, the program continues. But something
has happened: The computer has ignored Mr. Rimmer's \stw{BSC}, that
is, it has ignored the extra input. This
is because the \stw{INPUT} command doesn't really read a whole line
of input. Rather, it reads {\em one piece of information}. The
\stw{INPUT} command thinks that a piece of information ends at the end
of a line of input, or when it encounters a comma (\stw{,}) or colon
(\stw{:}) character.\index{, (comma)}\index{: (colon)}
\needspace{4cm}
If you want to include one of those symbols, you need to surround the
whole piece of information in double-quotes. So, if Mr. Rimmer had
read this guide instead of obsessing over the Space Core Directives,
he would have known to type \stw{"RIMMER, BSC"} (complete with the
double-quotes), to have the program
run correctly. It is important that the quotes go around the whole
piece of information, as otherwise the computer will think that the
first quote marks the start of a new piece of information. We can
see the difference it makes below:
\screenshotwrap{images/getting-started/input-quoting-1}
\needspace{1.5cm}
While this can all be a bit annoying at times, it has a purpose: The
\stw{INPUT} command can be used to read more than one piece of
information. We do this by putting more than one variable after the
\stw{INPUT} command, each separated by a comma. The \stw{INPUT}
command will then expect multiple pieces of information. For example,
we could ask for someone's name and age, with a program like this:
\begin{screencode}
1 PRINT "WHAT IS YOUR NAME AND AGE"
2 INPUT A$, A
3 PRINT "HELLO "; A$
4 PRINT "YOU ARE"; A; " YEARS OLD."
\end{screencode}
\needspace{4cm}
If we run this program, we can provide the two pieces of information
on the one line when the computer presents us with the \stw{?} prompt,
for example \stw{LISTER, 3000000}. Note the comma that separates the
two pieces of information, \stw{LISTER} and \stw{3000000}. It's also
worth noticing that we haven't put any thousands separators into the
number 3,000,000. If we did, the computer would think we meant three
separate pieces of information, \stw{3}, \stw{000} and \stw{000},
which is not what we meant. So let's see what it looks like when we
give \stw{LISTER, 3000000} as input to the program:
\screenshotwrap{images/getting-started/input-multiple-1}
In this case, the \stw{INPUT}\index{INPUT}
\index{BASIC 65 Commands!INPUT} command reads the two pieces of
information, and places the first into the variable \stw{A\$}, and the second
into the variable \stw{A}. When the program reaches line 3 it prints
\stw{HELLO} followed by the first piece of information.
Then when it gets to line 4, it prints the string \stw{YOU ARE},
followed by the contents of the variable \stw{A}, which is the number
3,000,000, and finally the string \stw{YEARS OLD}.
It's also possible to just give one piece of information at a time.
In that case, the \stw{INPUT} command will ask for the second piece
of information with a double question-mark prompt, i.e., \stw{??}.
Once it has the second piece of information. (If we had more than
two variables on the \stw{INPUT} command, it will still present the
same \stw{??} prompt, rather than printing more and more
question-marks.)
\needspace{4cm}
So if we try this with our program, we can see this \stw{?} and
\stw{??} prompts, and how the first piece of information ends up in
\stw{A\$} because it is the first variable in the \stw{INPUT}
command.
The second piece of information ends up in \stw{A} because \stw{A} is
the second variable after the \stw{INPUT} command. Here's how it
looks if we give this input to our program:
\screenshotwrap{images/getting-started/input-multiple-2}
Until now we have been asking the user to input information by using a
\stw{PRINT} command to display the message, and then an \stw{INPUT}
command to tell the computer which variables we would like to have
some information input into. But, like with the \stw{PRINT} command,
this is something that happens often enough, that there is a shortcut
for it. It also has the advantage that it looks nicer when
running, and makes the program a little shorter. The short cut is to
put the message to show after the \stw{INPUT} command, but before the
first variable.
We can change our program to use this approach. First, we can
change line 3 to include the prompt after the \stw{INPUT} command. We
can do this one of two ways: First, we could just type in a new line
3. The computer will automatically replace the old line 3 with the new
one.
But, as we have mentioned a few times now, programmers are lazy
beasts, and so there is a short-cut: If you can see the line on the
screen that you want to change, you can use the cursor keys to
navigate to that line, edit it on the screen, and then press
\specialkey{RETURN} to tell the computer to accept the new version
of the line.\index{Programmes!editing}\index{Programmes!replacing
lines}\index{Lines!editing}\index{Lines!replacing}
\needspace{4cm}
Either way, you
can check that the changes succeeded by typing the \stw{LIST} command
on any line of the screen that is blank. This will show the revised
version of the program. For example:
\screenshotwrap{images/getting-started/replacing-line-1}
\needspace{3cm}
We still have a little problem, though: Line 1 will print the message
\stw{WHAT IS YOUR NAME AND AGE}, and then Line 2 will print it again!
We only want the message to appear once. Thus we would like to change
line 1 so that it doesn't do this any more. Because there is no other
command on line 1 that we want to keep, that line can just become
empty. So we can type in something like this:
\begin{screencode}
1
\end{screencode}
\needspace{4cm}
We can confirm that the contents of the line have been deleted by
running the \stw{LIST} command again, like this:
\screenshotwrap{images/getting-started/deleting-line-1}
Did you notice something interesting? When we told the computer to
make line 1 of the program empty, it deleted it completely!
That's because the computer thinks that an empty line is of no use.
It also makes sure that your programs don't get all cluttered up
with empty lines if you make lots of changes to your programs.
It is also possible to DELETE a range of lines. For example (but don't do this now), you could delete lines 3-4 with:
\begin{screencode}
DELETE 3-4
\end{screencode}
You can read more about the DELETE command in the BASIC 65 Command Reference.
\needspace{4cm}
With that out the way, let's run our program and see what happens.
As usual, just type in the \stw{RUN} command and press
\specialkey{RETURN}. You should see something like this:
\screenshotwrap{images/getting-started/input-comma}
\needspace{2.5cm}
We can see our prompt of \stw{WHAT IS YOUR NAME AND AGE} there, but
now the cursor is appearing without any \stw{?} character. This is
because we put a comma (\stw{,}) after the message in the \stw{INPUT}
command. To get the question mark, we have to instead put a
semi-colon (\stw{;}) after the message, like this:
\begin{screencode}
INPUT "WHAT IS YOU NAME AND AGE"; A$, A
\end{screencode}
\needspace{4cm}
Now if we run the program, we should see what we are looking for:
\screenshotwrap{images/getting-started/input-semicolon}
\subsection{Exercises to try}
{\bf 1. Can you make the program ask someone for their name, and
then for their favourite colour?}
At the moment it asks for their name and age. Can you change the
program so that it reports on their favourite colour instead of
their age?
{\em Clue:} What type of information is age? Is it numeric or a
string? Is it the same type of information as the name of a colour?
{\bf 2. Can you write a program that asks someone for their name,
prints the hello message, and then asks for their age and prints
out that response?}
\needspace{2cm}
At the moment, the program expects both pieces of information at
the same time. This means the program can't print a message about
the first message until after it has both pieces of information.
Change the program so that you can have an interaction like the
following instead:
\begin{screencode}
WHAT IS YOUR NAME? DEEP THOUGHT
HELLO DEEP THOUGHT
WHAT IS THE ANSWER? 42
YOU SAID THE ANSWER IS 42
\end{screencode}
{\em Clue:} You will need more lines in your program, so that you
can have more than one \stw{INPUT} and \stw{PRINT} command.
{\bf 3. Can you write a program that asks several questions, and
then prints out the list of answers given?}
\needspace{2cm}
Think of several questions you would like to be able to ask someone,
and then write a program that asks them, and remembers the answers
and prints them out with an appropriate message. For example, running
your program could look like this:
\begin{screencode}
WHAT IS YOUR NAME? FRODO
HOW OLD ARE YOU? 33
WHAT IS YOUR FAVOURITE FOOD? EVERYTHING!
THANK YOU FOR ANSWERING.
YOUR NAME IS FRODO
YOU ARE 33 YEARS OLD
YOU FAVOURITE FOOD IS EVERYTHING!
\end{screencode}
{\em Clue:} You will need more lines in you program, to have the
various \stw{INPUT} and \stw{PRINT} commands.
{\em Clue:} You will need to think carefully about which variable
names you will use.
\section{Making simple decisions}
In the previous section we have learnt how to input text and numeric
data, and how to display it. However, the programs have just
followed the lines of instruction in order, without any way to decide
what to do, based on what has been input.
In this section we will see how we can take simple decisions using the
\stw{IF}\index{IF}\index{BASIC 65 Commands!IF} and
\stw{THEN}\index{THEN}\index{BASIC 65 Commands!THEN} commands.
The \stw{IF} command checks if something is true or false, and if it
is true, causes the computer to execute the command the comes after
the \stw{THEN} command.
The way the computer decides whether something is true or false is
that it operates on the supplied information using one of several
symbols. These symbols are thus called {\em operators}. Also, because
the compare two things, they depend on the relationship of the
things. For this reason they are called {\em relational}
operators.\index{relational operators}\index{operators!relational}
They include the following:
\begin{itemize}
\item Equals (\stw{=}). For example, \stw{3 = 3} would be true,
while \stw{3 = 2} would be false.
\item Less than (\stw{<}). For example, \stw{1 < 3} would be true,
while both \stw{3 < 3} and \stw{1 < 3} would be false.
\item Greater than (\stw{>}). For example, \stw{3 > 1} would be
true, while both \stw{3 > 3} and \stw{1 > 3} would be false.
\end{itemize}
As it is common to want to consider when something might be equal or
greater than, or equal or less than, there are short cuts for
this. Similarly, if you wish to test if something is not equal to
something else, there is a relational operator for this, too:
\begin{itemize}
\item Unequal, which we normally say as {\em not equal}\index{not
equal}\index{unequal}\index{<> (not equal to)}
(\stw{<>}). This is different to the mathematical symbol for not
equal, $\ne$, because the MEGA65's character set does not include a
character that looks like that. So the programmers who created BASIC
for the MEGA65 used the greater than and less than signs together
to mean either less than or greater than, that is, not equal to.
For example, \stw{1 <> 3} would be true,
while \stw{3 <> 3} would be false.
\item Less than or equal to (\stw{<=}). For example, \stw{1 < 3} and
\stw{3 <= 3} would be true,
while both \stw{4 < 3} would be false.
\item Greater than or equal to (\stw{>=}). For example, \stw{3 >= 1}
and \stw{3 >= 3} would be
true, while both \stw{1 >= 3} would be false.
\end{itemize}
A good trick if you have trouble remembering which way the \stw(<) and \stw(>)
signs go, the side with more ends of lines is the one that needs to
have more. For example, the \stw(<) symbol has one point on the left, but
two ends of lines on the right-hand side. So for something to be true
with \stw(<), the number on the left-hand side needs to be less than the number
on the right-hand side. This trick even works for the equals sign, \stw(=),
because it has the same number of ends on both sides, so you can
remember that the numbers on both sides need to be equal. It also
works when you have two symbols together, like \stw(>=), it is true if
the condition is true for any of the symbols in it. So in this case
the \stw(>) symbol has more ends on the left than the right, so if the
number on the left is bigger than the number on the right, it will be
true. But also because the \stw(=) symbol has two ends on each side,
it will be true if the two numbers are the same.
\needspace{2cm}
Using these relational operators, we can write a line that will do
something, but only if something is true or false. Let's try this
out, with a few examples:
\begin{screencode}
IF -2 < 0 THEN PRINT "-2 IS A NEGATIVE NUMBER"
IF 2 < 0 THEN PRINT "2 IS A NEGATIVE NUMBER"
IF 0 < -2 THEN PRINT "-2 IS A POSITIVE NUMBER"
IF 0 < 2 THEN PRINT "2 IS A POSITIVE NUMBER"
\end{screencode}
\needspace{4cm}
These commands work fine in direct mode, so you can just type them
directly into the computer to see what they will do. This can be
handy for testing whether you have the logic correct when planning an
\stw{IF} -- \stw{THEN} command. If you type in those commands, you
should see something like the following:
\screenshotwrap{images/getting-started/if-then-less-than-examples}
We can see that only the \stw{PRINT} commands that followed an
\stw{IF} command that has a true value were executed. The rest
were silently ignored by the computer. But we can of course include
these into a program. So let's make a little program that will ask
for two numbers, and say whether they are equal, or if one is greater
or less than the other. Before you have a look at the program, have
a think about how you might do it, and see if you can figure it out.
The clue we will give you, is that the \stw{IF} command also accepts the name of
a variables, not just numbers. So you can do something like \stw{IF A
> B THEN PRINT "SOMETHING"}. The program will be on the next page, to stop you peeking before you
have a think about it!
\pagebreak
Did you have a go? There are lots of different ways it could be done,
but here is what we came up with:
\begin{screencode}
1 INPUT "WHAT IS THE FIRST NUMBER"; A
2 INPUT "WHAT IS THE SECOND NUMBER"; B
3 IF A = B THEN PRINT "THE NUMBERS ARE EQUAL"
4 IF A > B THEN PRINT "THE FIRST NUMBER IS BIGGER"
5 IF B > A THEN PRINT "THE SECOND NUMBER IS BIGGER"
\end{screencode}
We can then run the program as often as we like, and the computer
can tell us which of the two numbers we give it is biggest, or if they
are equal:
\screenshotwrap{images/getting-started/if-compare-variables-1}
Notice how in this program, we didn't use fixed numbers in the
\stw{IF} command, but instead gave variable names instead. This is
one of the very powerful things in computer programming, together
with being able to make decision based on data. By being able to refer
to data by name, regardless of its current value or how it got there,
the programmer can create very flexible programs.
Let's think about a bit of a more interesting example: a ``guess the
number'' game.\index{Guess the number}\index{Games!Guess the number}
For this, we need to have a number that someone has to guess, and then
we need to accept guesses, and indicate whether the guess was correct
or not. If the guess is incorrect, we should tell the user if the
correct number is higher or lower.
We have already learned most the ingredients to make such a program: We
can use \stw{LET} to set a variable to the secret number, \stw{INPUT}
to prompt the user for their guess, and then \stw{IF}, \stw{THEN} and
\stw{PRINT} to tell the user whether their guess was correct or not.
So let's make something. Again, if you like, stop and think and
experiment for a few minutes to see if you can make such a program
yourself.
Here is how we have done it. But don't worry if you have done it in a
quite different way: There are often many ways to write a program to
perform a particular task.
\begin{screencode}
1 SN=23
2 PRINT "GUESS THE NUMBER BETWEEN 1 AND 100"
3 INPUT "WHAT IS YOUR GUESS"; G
4 IF G<SN THEN PRINT "MY NUMBER IS BIGGER"
5 IF G>SN THEN PRINT "MY NUMBER IS SMALLER"
6 IF G=SN THEN PRINT "CONGRATULATIONS! YOU GUESSED MY NUMBER!"
\end{screencode}
\needspace{4cm}
The first line puts our secret number into the variable \stw{SN}.
The second line prints a message telling the user what they are
supposed to do. The third line asks the user for their guess, and puts
it into the variable \stw{G}. The
fourth, fifth and sixth lines then check whether the guess is correct
or not, and if not, which message it should print. This is done by
using the \stw{IF} command and an appropriate relative operator to
make each decision. This works well, to a point. For example:
\screenshotwrap{images/getting-started/guess-number-1}
We can see that it prints the message, and it asks for a guess, and
responds appropriately. But if we want to guess again, we have to use
the \stw{RUN} command again for each extra guess. That's a bit poor
from the user's perspective. However that is unlikely to be a problem
for long, because the user can see the secret number in the listing on
the screen!
So we would like to fix these problems. Let's start with hiding the
listing. We previously mentioned that when the screen scrolls,
anything that was at the top of the screen disappears. So we could
just make sure the screen scrolls enough, that any listing that was
visible is no longer visible. We could do this using \stw{PRINT} and a
\stw{FOR} loop. The screen is 25 lines, so we could do something
like:
\begin{screencode}
FOR I = 1 to 25
PRINT
NEXT I
\end{screencode}
But there are better ways. If you hold down \specialkey{SHIFT},
and then press \specialkey{CLR\\HOME}, it clears the
screen. This is much simpler and more convenient. But how can we do
something like that in our program? It turns out to be very simple:
You can type it while entering a string! This is because the keyboard
works differently based on whether you are in {\em quote
mode}\index{quote mode}.