 So recently my 7 year old son was looking at my laptop and an app that I'm working on was the last thing I had opened so that was on display and he asks me, Daddy is this the app you're working on? I was like, yeah. He says, that's cool. Can you teach me how to code? And I was like, can I? And what better way to start learning to code than go all the way back to the mid 80s where it all started for me on the humble Commodore 64, where it's just you and the basic interpreter, as soon as you power it on, it just sits there waiting, expecting input from you, the user, and when I started out, you just had to grab the manual, start typing things in and kind of figure it out as you went along. I thought the best place to start would be with the ubiquitous 10 print Hello World 20 go to 10 run. I think this is one of the first programs all of us learn when we were first getting started with basic. OK, so before moving on to something a bit more interesting, I just wanted to go back right to the start to step through that simple program that we saw before. And so if you're a complete, you know, completely new to programming and new to the Commodore 64, other 8-bit micros, this will give you a small footing in programming and what's to come. So there are a couple of concepts that you can learn from that very simple program. So in that previous program, there was two commands, there was print and go to. So they're both very simple and do almost what they say, especially in the case of go to, but print. So if I just put this statement here, don't forget the exclamation mark. So print, just print whatever's in the speech marks that come afterwards or anything that comes afterwards, prints it to the screen once you press return. So there we go. It executes immediately Hello World. And that's all well and good and it's good fun for getting used to typing things in on the computer and if you make mistakes, the computer will tell you so you can go back and check your code and see where the mistake was and you can see I've missed off the T from print. So print is not a valid basic command and you get the syntax error, syntax, which means it's an error in what you've typed. And these commands are all part of the basic programming language on the Commodore 64. The basic stands for Beginners All Purpose Symbolic Instruction Code. And it's where I would say the vast majority of programmers, I don't know if they still do, but they certainly did through the 80s and 90s, you probably start with basic because it was built into a lot of the home computers at the time. So that's just the one you were exposed to first on the manual, which would have examples of and teach. So yeah, so basic and it's quite powerful, but back in the computers at the time, it's very slow compared to another programming language where I'm going off topic here of it. So yeah, so basic is a good all round programming language that we get used to the programming concepts, which you can use later in other on other computers and everything, the concepts are the same even if the commands change a little. So when you go back and look at that program that I typed before, the print command was prepended or had a number before it, but why is that? Well, by doing that, when you press enter, you're telling the computer that you don't want it to execute that command immediately, you want to store what you've written on that line in memory location 10. So why 10? You know, one, two, three. Well, at the time I didn't question this, it was just how it was written in the manual and I just copied it along blindly. And it was only later when I started to create my own programs, I realized that I wish I could go back and put in something that I've missed out there or add a feature or something like that. And back then you couldn't just copy and paste and you couldn't just move the cursor around and enter a couple of blank lines. So what you'd do is you'd pick a number between the two points where you wanted this new command to be executed and just do that. And what the computer would do, because the computer executes the program in order, in sequence of the numbers, no matter what the sequence is, it could be a completely random kind of sequence, one, five, eleven, thirty-two, but it would still execute them in order. And if you put in a number that's between two other numbers, and we type list, which is the command for listing the basic program currently in memory, the computer would have ordered them for us. So that's why we leave a space and increment the line numbers that it intends. Now if we run that, you can see both statements printed before the go to is hit, which just sends the computer back to the line number afterwards. So that's all line 20 does go to. So if I expand through there, go to this line number. So we start at 10, print's high, so go to 15, print my name is Bob, get to 20, which sends us back to the start, and that's why we get that infinite loop. So that's all well and good. You've learned a couple of concepts here of memory locations, being the line numbers, two commands, and messages in speech marks afterwards. So let's just move on to something a little more interesting now, which is the game that we created. So here's almost the full listing for the game that me and my son created, the very simple maths game. I'm going to go through this section pretty quickly. There's a lot to cover, and I'm going to go through it fast, just to try and keep the video length down. Anything that I go over a bit too quickly, by all means, that's the beauty of video, you can just go back and repeat that section, and by all means I'll see any questions or comments in the below, and I'll get back to you. So what we can see here, here's almost the full listing. If I type list, you can see here's the game, but there's not enough space on the screen for it to all fit on to go through. Text editors are open here with the same source code, but black and white sets are nicer contrast, and it all fits on the screen. So I'm going to whip through, and then we're going to go through the game, and you can see the finished product. So I always start at line 10, but what the print statement does is, from wherever the cursor is on the screen, it'll just print from that point. So you'll be asked 10 edition questions, blank line, choose your difficulty. Now it became apparent very quickly, and as you'll see here, I run it, there, it just starts from wherever I type run, it just outputs those print statements immediately below. You can still see all the other junk that's on the screen above, and it just doesn't look very nicer. So to make things a bit nicer, I went back and put in line 5, print CHR dollar, and then 147 embraces, and that's the effect of clearing the screen of all texts. So if you want to print a specific character that either isn't accessible by pressing shift and something on the keyboard, or you don't know what the key combination is, but you've looked up that particular character, the number for that particular character, each character has its own unique number. You can just type print CHR dollar symbol, and then that number, and then I have the effect of printing that character to the screen. On the Commodore 64, and probably others, there are special characters that don't actually print something to the screen, but they have an effect on the screen, and number 147 has the effect of clearing the screen of all texts. So we do that, so we've got a nice clear screen, and I'll just go back and demonstrate that. I need to break out the program, so pretend you haven't seen this. So if I list just up to line 20, you can see it's not in there at the moment. I'm going to teach you something else here as well. You'll be typing print an awful lot probably when you're making basic programs, because that's how you tell the user something. You print it to the screen. So a shortcut for typing the word print is a question mark, believe it or not. And then also, you don't need to leave spaces there just for human readability, so you can see up here, print, and then the speech marks appear immediately afterwards, because the computer doesn't care, just for human readability. So I can do that, and then high, and that would be perfectly valid. And when I list the program again, if I just list line five, you can see the computer's turned that question mark into print. But let's overwrite that now, CHR dollar, open bracket 147, close bracket, enter. So now when I run, we clear the screen. Okay, back to the code. So we clear the screen, and then we get on with asking the questions. You can see here that there's a colon. Well, why is that there? And what that does is it breaks a line up. It tells the computer that what comes after the colon is a brand new command as if it was on its own line. And if you've got a lot of the same command, in this case, we've got three print statements on one line. To me, it just seems a bit messy and poor practice to put each one on its own line, each print statement. You can do it, and that's perfectly valid, and that's indeed how I started out. But when I learnt that the colon can split a line into different commands, I prefer to do it that way. So I tell the user we ask 10 edition questions, then I leave a blank line. Again, just for readability, you can see that here, 10 edition questions, and we've got a blank line. Choose your difficulty, and then these two lines are just printed immediately after. So choose your difficulty, and then print one easy. So press one for easy, too hard. Then we're asking the user for input. And as you might expect, input does exactly that. It'll put a question mark on the screen with the blinking cursor afterwards, which you can see here. So we know that input statement has been reached, and when the user inputs something, we need to store it somewhere, and you store it in what's called a variable, which is just a bit of computer memory that can have something in it that changes. So it varies a variable. And on the Commodore 64, it gets to give a variable a name, and it can be the full word. So DI is short for the word difficulty, but the computer will only actually use the first two characters. So you can have a maximum number of variables of 26 times 26, whatever that is, and which I would have thought is more than enough. You can reuse variables as well, multiple times throughout a program, if it's safe to do so. But because the computer only uses two, I've abbreviated it down to two. This dollar symbol afterwards means that anything could appear in there. So it's not, if that wasn't there, the computer would be expecting a number, and if I didn't give it a number, it would ask the question again, because it would be an error. It wouldn't know how to handle it. So if I put a letter rather than a number, it would ask the question again, if I put a dollar symbol, it'll know that it can accept anything put, and it'll put it in that memory location, DI. So then when we want to use DI, we want to use what's in that memory location, whatever the number is, one or two, we can use reference DI, dollar. That's exactly what we do here. So line 30, we have our first if statement. So the if tells the computer to check something. If it's true, if the result is yes, that is true, then do this. So we can see here, if DI dollar is equal, so if the person has entered whatever is in the speech box here, one, then do this. And in this case, we're initializing another variable without the dollar symbol, so it must hold a number. We're initializing D with the value of 10. So a bit of algebra, really. If that is not true, then we check again, we ask if DI dollar is equal to two, and if so, then do this. We initialize D with 50. Now, you might be thinking, well, what if the user puts in a zero or a three, they're the valid numbers, but we're not checked for them. I'll get to that in a moment. The program won't crash. In fact, it's a bit of a cheat, really, when playing the game. I'll come to that in a moment. Line 50, I could have put this before, really, but as we're initializing variables here in this section, I'll put it here. So initializing another couple of variables. This one is short for question, and that's gonna store the number of the question that we're on. I've initialized that to one, and SC, I've initialized to zero. Now, if you use a variable before initializing it, you won't get an error, and the value will default to zero, but that's why it's just good practice to declare them first. Hi, it's future me here. What I was trying to say in this section, and just not finding the words, is that if you don't initialize a variable to zero before you use it, and you've used that variable in the program, and you've rerun the program, it could well still have the last value that was stored in there, still in there. So if, say, it had number 47 in there, and you rerun the program and didn't initialize it to zero again, when it used the variable SC, and it had the value of 47, you might get an unexpected result. So I'm gonna start on question one, because computers like to count from zero. So zero to nine is 10 digits, but us humans used a decimal, we'd like to go from one to 10 to ask 10 questions rather than zero to nine. Lines 60 and 70 are identical, except for one side of A and one side of B. So we're initializing another couple of variables here, one called A, one called B, and they must store a number, because there's no dollar symbol, and we want to store in A and B an integer, so a whole number, no decimal points here, so a whole number, that number, we're asking the computer to generate a random one, and R and D is the way you tell the Commodore 64 to produce a random number, you then need to give it what's called a seed value, right? And we need to tell it where to get the random numbers from this three, you can code many, but there's three built into the random function on the Commodore 64, and that's negative one, zero and one. I'm not gonna go into the details now, you can find that out very easily online, but one gives you the most random numbers, okay, the most random of the random number generators, and what comes next after the, is an asterisk, to tell it if there's an upper limit on that random number that you want to generate, in this case, we do want it to be between zero and whatever is in D, they're zero and 10, and it's not inclusive, you'd only get zero to nine, okay? So the random number, whatever the upper limit is, it doesn't include that in that range of random numbers it can produce. To get around this, we say, well, the whole thing you've produced there, add a one to it, shift it up by one, so now you'll get a random number between, if the user's chosen one and D's got 10 in it, you'll get a random number between one and 10, okay? If the user's chosen two, then do you have 50 in it, you'll get a random number between one and 50, so that's why it makes it a bit more difficult, you can still get a sum of two digits between one and 10, but the range is a lot wider, so it's likely to be harder, over 10 questions, you're likely to get higher numbers. Okay, so let's go revisit what happens if the user's entered zero or into three. Now, if that is the case, then D will never get initialized with anything, if one or two are not true, if DI dollar doesn't hold one or two, then this part, the then D equals whatever, never gets executed, so this D is never initialized with a value, so the computer would be using seeing D here for the first time, and if D hasn't been initialized with a value, it gives it zero, then we're shifting the whole thing up by one, so it has the effect actually of making every question of the 10, one plus one, so if you just wanna cheat the game or get through it quickly, and with a perfect score, enter anything other than one or two, any number other than one or two, and that's what happened. So anyway, once we've done that, we've set up our variables, we've got a random number stored in A and B, we're gonna clear the screen again, this is exactly the same as line five, and then we're gonna say, right, print Q, so print the number stored in Q, the first time we run it will be one, so print Q, and then print what is A, where it was stored in A plus B? Hang on, what are these semicolons doing here? We know what the colon does, that breaks the command into a new one. You know, I don't think I need that one there, it'll probably work just fine, like that. Okay, so what does the semicolon do? Well, one thing I haven't explained yet is that the Commodore 64 and many others, computers of the era, when a print statement is executed, it would automatically add a carriage return and line feed, now what is that? That's old typewriter talk, actually, what it means is the equivalent of press and enter, you press enter, and the cursor that was here, you see it flashing here, you press enter, and we go down one line, and return all the way to the start, so after a print statement, the computer will do that, unless you put a semicolon, that tells the computer, after this print statement, don't do a carriage return and a new line. I want what comes next to appear adjacent to, follow on from, what I've just printed, so we get the question number, and then what is, and then the value stored in A, then we'll put a plus symbol, and then the value stored in B. Then we're asking the user for input, which we're gonna store in C, C with no dollar sign, so that must be a number, and if we don't enter a number, we'll just ask the question again and again. Once the user has entered a number, we've got another compare statement here now. Let's, yeah, let's break this down. So if C holds the value of A plus B, so if the user's got it right, so if C is equal to the sum of A plus B, then go to line 200. If C is not equal to A plus B, then go to 300. Now the reason I've had to do this is because if it's not equal to C then obviously, this is gonna get executed, but the code won't know what to do, it'll just continue going down anyway, so it will hit 200. So we do have to test for this so that we can skip over with this go to, we can skip over 200, 210, 220, and go to 300. So if the user's got it right, got the sum right, we go to 200. Hi, it's future me here again. Upon reviewing this video whilst putting it together, it's become apparent that it could actually do away with line 100 entirely because we don't need to test if the answer is right. We just need to test if it was wrong. If it was right, it can be assumed that it was right and line 200 would execute, and only if it's wrong do we need to skip over that part. And what 200 does here is it increments the value that's in Q by one, so if I ask the question, we now need to go into question two, so whatever is stored in Q, so what we're saying is Q is now equal to whatever is already stored in Q plus one. So we know it's got one at the moment, the first time through. If I ask the first question, we're now gonna store Q plus one, so we've now got two in there. And as we got it right, we're also gonna increment SC short for score. We're gonna increment score by one. The score was zero, so if the user's got it right, they've now got one point. Then we only need to check to see if we've reached question 11. If we've reached question, if we have reached question 11, that means we've asked 10 questions already, in which case, that's the end of the game, and we go to 400, which handles the end of the, you know, we'll come to that in a moment, which handles it once we've asked the 10 questions, 10th question. If not, then we're gonna go to 60, which sets up another couple of random numbers and ask the question again. And again, once the answer's been made, we'll check and see if Q is 11. If not, we're gonna ask question again. We've got that loop. If the user's got it wrong, we skip over in line 300 to 320, it's the same as 200 to 220, except we're not also increment in the score by one. So if the user's got it wrong, the question number can go up, but not the score. Now, once we've asked all 10 questions, we're gonna jump to 400. So whether they answered the last question right or wrong, once we've hit the 10th, once we've asked the 10th, we're gonna come down here to 400. And 400, we leave a blank line, then we said you scored, and then adjacent to you scored, we're gonna output the score out of 10. So we're 10 questions. And then leave another blank line. To play again, press Y then enter, or any other key then enter to quit, another blank line, and then we're gonna ask the user for an input, a string, so we've got Q dollar. So I've reused Q, and I've now changed it to asking for it. It doesn't matter now. Now that we know we've reached the end, I can reuse Q quite happily because it's served its purpose up here already. I'm not gonna mess anything up. So we ask Q, we're gonna ask the user for input. If that input is equal to Y, we're gonna go to five, which starts the whole thing all over again. And if not, then it's just gonna print. Thanks for playing. And we're gonna be dumped out to basic. So let's just have a quick run through. Oh, it's asked 10 easy questions. So I've done it myself, 10 plus six, 16, one plus seven, also 16, five plus five, 10, three plus two is five, five plus seven is 10. You get the gist, what would that be? A little block. Let's get one wrong. You scored nine out of 10. Play again, press Y then enter, any other key to quit. So we're gonna say Y, two for hard. And here we go. Well, that was a simple one. That's just the luck of the draw. But here we go. Something a little more challenging. Let's see if I can get all of these right. Put myself under some pressure here. Oh, no. What am I doing? Yay, 10 out of 10. Okay, so let's just go back one more time and enter something other than one or two. And we can see. The reason that has happened, as I said, it would be one plus one for any question because D hadn't been initialized. That's because D had already been initialized previously and the value in D, the old value, will still be there. So it doesn't reset it to zero. If it hadn't been initialized, it would have been zero. And then with one added to it, it would have made every question one plus one. If it has been initialized already, then it's still gonna hold that previous value. If that makes sense. Let me try it again. Yeah, here we go. So I've run the program a new by saying, no, don't wanna play again in typing run. Put in a value other than one or two and D has just got a one in it. So we're asking for a random number of an upper limit of zero. So from zero to zero and then we're gonna shift it up by one. Well, we're gonna add one to whatever that the result was. And yeah, so here we have it. There's the full game, full listing. And just in closing here, hi, future me here again for the last time. Something that I haven't mentioned and I only became clear just now when reviewing the video and putting it together is that the line I'm bringing out, I said that always go up in increments of 10. So that leaves me space to go back and insert something that I might think of later. But you can also see I've jumped from 100 to the 200 to 300 to the 400s. I also, if there's distinct areas of code that do something different that I might wanna expand on later, I might move them far away from each other just to give myself even more space to work with later. I just wanna ask, I just wanna pose a couple of questions. Obviously, if you have any questions or comments so far, please leave and I'll get back to you. Please like and subscribe as everyone says. But if this has inspired you to do your own program and maybe you wanna modify this in some way, which wouldn't, there's a couple of obvious ones. An easy modification you could make to this and I'd love to see your results. So do let me know, but would be to change from 10 addition questions to 10 multiplication questions. And that'd be quite a simple change to do. If you want more of a challenge, make it 10 subtraction questions. Now I'll say more of a challenge because unless you wanna have negative numbers, you would need to ensure that A is greater than B before the question is asked. So if you've got two minus 10, you know, the answer being minus eight, that might be pretty difficult, you know, depending on the age of the person that's gonna be answering the questions. But 10 minus two, you know, is a lot more obvious because it's still a positive number, the answer. And if you wanna super challenge 10 division questions. So that'll do for me for now. Hope you've enjoyed the video and I look forward to making another one in the future.