Hi and welcome to lesson 3. This time we are going to make once again a bit complex program than last time. Well, let's make more complex than a bit. There are going to be more than one command you learn and we do some other interesting stuff too. =) Here is the code we study this time. Type it on your QBasic and Run it. After that, let's examine it and what it does.
1 ' Here we start our program. CLS COLOR 8 LOCATE 10, 10: PRINT "This program examines you as you examine this program." LOCATE 11, 10: INPUT "Write your name and press ENTER"; Name$ CLS COLOR 11 LOCATE 10, 10: PRINT "And now this program wants to know more about you." LOCATE 11, 10: INPUT "Type your age and press ENTER"; Age CLS COLOR 3 LOCATE 10, 10: INPUT "Write the name of your mother and press ENTER"; Mother$ LOCATE 11, 10: INPUT "Now tell me how old your mother is"; MotherAge CLS LOCATE 10, 10: PRINT "Well hello "; Name$ LOCATE 11, 10: PRINT "You are son of "; Mother$ LOCATE 12, 10: PRINT "Age difference between your mother and you is "; MotherAge - Age; " years." LOCATE 13, 10: PRINT "Now if you wana do this again, press ENTER. Else press CTRL and C keys." SLEEP GOTO 1
Now hold on! There is long code and lot's of new commands that I don't know!
These kind of feelings came in to your mind?
Don't worry. You have already learned some commands and it has been pretty easy, doesn't it? So let's look what stuff are in that code and before we end this lesson, you have learned them all, trust me =)
New commands are COLOR, LOCATE, SLEEP and GOTO. There is also some weird line 1 ' Here we start our program also.
You have probably noticed familiar commands PRINT, INPUT and so on also in that code? Familiar Name$ is there also so I bet you figured out why Mother$ is there ;) MotherAge and Age might look weird there but they work same way than Name$ and Mother$. Difference with these variables is that some of them are missing $'s, right?
At this point im not gona start explain you how variables works. There are so many and good variables tutorial already made by others that im not gona explain variables any deeper at this series of lessons. I only say this. Variable with out $ at end of it is only for numbers to store. If you want to store letters etc. then you have to use $ at the end of variable. Like this'
Number is for numbers only. 1,2,3,4,123123123,12342341234123424 are ok but you cant store My name is Kristian on Number. Number$ can be used for letters and numbers. So you can store number 3 or 12312312 there and you can also store your name there.
It's no matter how you name your variable. Name$ or BlaaBlaa$, QBasic don't care =)
This ain't the absolute truth or even used way what comes peoples who have coded with QBasic more than few days. But im not gona go there, I only mess your head and as I told, there are already great tutor's about how to use variables so im not gona explain more of them. Let's stay on very basics now.
Ok, back to that program. Let's start with this.
COLOR (some number here)
COLOR command tells QBasic what color you want to use. That color stays in use until you give QBasic another COLOR command. You can use CLS but still what are printed after it, will be printed with that same color. Check HELP on your editor. Locate COLOR and study what colors you can use ;) Then change numbers after COLOR commands and see what happens. So I think we are ok with COLOR now. Let's check next new command.
Ok, simple command. SLEEP pauses program for a while. How long? It depends how you use it. SLEEP 1 makes program sleep for a second or until you press some key. SLEEP 5 makes program sleep for a five seconds or until you press some key. SLEEP well, here is no seconds ordered so how long it will sleep? As long you press some key. Try it, use SLEEP with out parameters. Go make some coffee, maybe in store to buy something and watch a movie. Your program is still sleeping =)
And why I put that SLEEP command there? Well, as you notice. After the last printing part of program there is still one command. GOTO. With out SLEEP program does that GOTO command and we wouldn't see those last printed text's. So SLEEP give's us time to check what read's on screen. If you use SLEEP 5 instead of SLEEP then you have five seconds before that GOTO does it's tricks. And that leads us to GOTO command.
GOTO does what you probably assume that it does. It goes somewhere. Where? It depends where you order it to go. GOTO 1 makes program jump into 1. And where is that 1?
1 ' Here we start our program.
You got it? =) So GOTO goes at start of our program and it ask's you those same guestion again. On this program the only way to break out of that GOTO command is to press CTRL and C together. Goto is usable at beginning of QBasic learning. When you learn more, you might notice that GOTO is pretty useless =)
But we are learning basic's here so let's not care about that. Test what happends if you change that GOTO 1 to GOTO 2. Ok, one more to go. It might look like a monster.
LOCATE 10, 10 : PRINT "something in here"
Well, let's make it look a bit simpler.
LOCATE X, Y That's what we learn now. You know what PRINT or INPUT does after it so no point check them more. LOCATE command tells to QBasic that there is certain place on screen where you want to go. X symbols here the place on UP and DOWN line, and Y symbols LEFT and RIGHT line.
LOCATE 1, 1 goes to top left corner of screen. Try this.
CLS LOCATE 1, 1 : PRINT "Here"
and after running it, change those numbers after LOCATE command. Put 10 instead of 1. Yeah, in both of them =) And after that put 1 as a first number and 15 as a second number.
There is still one thing I wana go thru with you. LOCATE 12, 10: PRINT "Age difference between your mother and you is "; MotherAge - Age; " years.". Does that feel like a monster to you? It's ok if it does so. But examine it a bit. There is that LOCATE command I just told you and after it is : mark. Well it makes QBasic to LOCATE that place and after that comes PRINT command. Simple? Both are familiar commands.
; MotherAge - Age; " years."
Ok, this might seem a bit messy but not for a long.
MotherAge is a value you typed when program asked your mother age and Age is a value you gave when program asked your age. So if your mother age is 50 and your age is 20 then that same line could look like this ; 50 - 20; "years."
Now, use time to study all those commands we have learned. Spent time with them! When studied enough you should be able to do your own program that ask's all of your family members names, ages and it can calculate how old you and your family are together and so on. Make new variables like a Address$, Phone and so on and most of all, try use them so many ways you just can imagine =)
There's lesson number 3. In next lesson, we are once again gona break your limits with QBasic.