Languages : QBasic
Tools : QBASIC.EXE
Statements : Do, Loop, While, Until, Print, Line Input, Dim
Prerequisites : Install QBasic (from Tools CD #3 \ Microsoft Tools (part 2 of 2)
\ QBasic - simply copy it onto your hard disk)
Estimated time : 20 minutes - not including "For
Fun".
Conceptual Introduction
How does the firing action in a pistol work? How does a combustion engine (such as in your dad's motor car) work? If you just stand by and watch, things will happen too quickly for you to be able to make out the sequence of events. But if you take a video camera and you film the pistol being fired or the engine in operation, you can review the recording frame by frame and get a much better idea of what's going on. In fact, modern golf instructors often use exactly this technique to help show their pupils any problems with their golf swing. It's one thing to say "You're bending your left arm too much at the height of the back-swing", but to actually show them on-screen exactly what they're doing can be very helpful.
Or have you ever played with dominoes, setting up a big long sequence of standing dominoes, tipping one and watching the rest collapse? A domino series falling is just slow enough for the careful observer to see what happens and why, but a computer runs far too quickly for anyone to be able to watch it as it goes. So is there any way we can use the "frame by frame" approach of a video camera to 'slow it down' and watch exactly what it does? Fortunately for us, there is! Now in the same way that golfers survived for decades (centuries?) without the aid of a video camera, programmers survived for some years without the aid of "breakpoints" and "single-stepping" (which we'll introduce in a moment). So it's perfectly possible to program and to program well without the tools we're about to introduce. But believe you me, when you know how to use these tools correctly, it will be immensely valuable, so with no further ado, let us proceed...
First, A Simple Program
Start QBasic and enter the following program.
Dim Answer As String
Print "Hello there!"
Line Input "Is it a great day? ", Answer
Print "Well, I'm not sure if I agree with you, but you said '" + Answer + "'."
Now press F5 to run the program, and see how it works.
Notice how we have an "output" screen and an "IDE" screen? (The IDE screen is the blue-background screen where you type your program. "IDE" stands for "Integrated Development Environment".)
For starters, once the program is finished and you're back in the IDE, press F4. Voila! You can see the output screen again. Press any key and you're back in the IDE. Why is this useful? Trust me - there will be times that it's useful to review the output of your program after it's finished running. But more importantly, this allows you to switch between the IDE and the output screen while your program is running, provided you're in single-step mode (we'll get to that very soon now).
So, move the cursor to the second line of your program ("Print "Hello there!"") and press F9. This sets a 'breakpoint' on that line, evidenced by a red background for that line instead of the usual blue. Big deal? Well, try running now - press F5. Instead of seeing the output screen like you're used to, you are still looking at the IDE, but the line "Print "Hello there!"" is now in bold! What's happening? Press F8 ONCE, and if you look carefully, you'll notice that the emboldening is no longer on the second line, but it's on the third line! You are watching the computer work through your program, literally step by step!
Note the red background on the line with a "breakpoint".
What does this bold-white highlighting mean? It means that line is the "current" line of "execution". Yes, "executing" your code simply means running it, not putting it on the gallows!!! :o)
After pressing F8, the current line changes, and with it, the bold-white highlighting moves to show the new current line.
Now things start to get a little trickier. This is where you start juggling backwards and forth between the IDE window and the output window. Press F8 again! What happens? Suddenly you're at the output window again, being asked to answer the question as to the wonder of this day! But F4, which used to let us switch between the IDE and output screens, doesn't work! Why not? When your program is waiting for input from you, the only way to interrupt it is by pressing Ctrl + Break (detailed in a moment). F4 only works in single-step mode when your program is not waiting for you.
Let's do a few simple exercises :
Press Ctrl + Break (meaning hold down the Ctrl key on your keyboard, then press the Break key (which can be hard to find - usually towards the top or top right of your keyboard, and often sharing the same key as the Pause key), then release the Ctrl key after releasing the Break key). What happens? Suddenly you're back in the IDE, and there is that bold-white highlighting again, showing the "current line". Pressing Ctrl + Break interrupts the program and puts it in single-step mode. (Notice that the current line is the line after the line which asks what you think of the day. When you use Ctrl + Break at a time when the program was waiting for you to say something, the computer treats it as if you said nothing at all, so that it can finish that instruction before going into single-step mode. This is because otherwise QBasic would have to have some fancy way of showing that an instruction is half complete (e.g. in the case of "Line Input", that the output step has been done but the input step is still underway), and this would have introduced many more complexities.)
Now that you're in single-step mode again, press F4 a few more times. You can see that, in single-step mode, a) there is ALWAYS a line of code highlighted in bold white which is the 'current' line; and b) you can freely switch backwards and forth between the IDE screen and the output screen by pressing F4.
Press F5 and you'll see that the program goes back into "freely running" mode until either it ends or until you interrupt it with Ctrl + Break again. (Of course, this program is so short and simple that the program will finish running well before you have a chance to press Ctrl + Break, but soon we're going to make some programs where Ctrl + Break is very useful!)
Summary : The most important keys to remember are F9 (in the IDE to set a "breakpoint" where the program will go into single-step mode), F8 (in single-step mode to advance one step), F5 (to start the program from the beginning, or if already in single-step mode, to return to freely-running mode), and Ctrl + Break (important when you need to interrupt your freely-running program).
Brief Introduction To Loops
Consider the following program :
Dim Answer As String
Do
Line Input "What is the password? ", Answer
Loop While Answer <> "let me in!"
Print "Alright, I'll let you in."
What do you think the program will do? Once you've had a guess, run it and find out!
Notice a few interesting features of this program. 1) The password is case-sensitive. That's why "LET ME IN!" didn't work, even though "let me in!" will work. 2) It is very ugly that the program simply asks for your password again without first saying "Password incorrect. Please try again.". Some of you will already know how to fix this, but to avoid confusing those who are not familiar with the necessary programming constructs, I've kept this program very simple. 3) If you forget the password? Ctrl + Break!
So what's happening here? I reckon you're all brainy enough to read the "source code" of the program, look at how the program is behaving, and figure out for yourself what the "Loop While ..." bit means. Some of you may be asking, "What's that "Do" line?". The "Do" line marks the beginning of the "block" of code to repeat, and the "Loop" line marks the end of that block of code. Huh? In other words, we could have multiple lines that we are repeating. For example, let's change the program to make it extra tough for would-be intruders :
Dim Answer1 As String
Dim Answer2 As String
Do
Line Input "What is the first password? ", Answer1
Line Input "What is the second password? ", Answer2
Loop While Answer1 <> "let me in!" Or Answer2 <> "ditto that!"
Print "Alright, I'll let you in."
Run it, and look at the "source code" carefully. Can you see how the "Do" and the "Loop" work together?
Using Single-Step Mode To Understand Loops
So what if you want to get a better idea of what's going on - what if you want to "play it in slow motion"? Easy! Move the cursor to the "Do" line and press F9 to set a breakpoint there.
Then press F5 to run. Almost instantaneously the "Do" line is highlighted in bold-white, meaning it is the current line.
Press F8. Which line is current now? How can you tell? And why did that line become current, not some other line?
Press F8 again. Suddenly you're at the output screen, and you're being asked for the first password.
Make some answer and press Enter. Suddenly you're back in the IDE. Which line is current now? Why?
Press F8 again, and once again you're at the output screen. Answer this second question, and press Enter again, and now you're back in the IDE, and the "Loop While ..." line is the current line. What do you think will happen next time you press F8?
Is there a way to know?
Could it be useful to find out what values are in "Answer1" and "Answer2" at this point?
Press F8 and see what happens.
In Conclusion
Some of you already knew all this stuff, but for some of you it was new and perhaps a little hard at first. The sample programs here were really simple and you hardly needed Breakpoints and all these fancy powerful tools for such a simple little job - kinda like using a powersaw to cut a blade of grass - but these same skills you've begun to use on these simple programs are just as usable, and much more useful, on large and complex programs. Furthermore, almost every programming language ("IDE" to be technically strict, but I don't want to confuse those who don't yet understand the difference) has these "freely running" and "single step" modes and breakpoints too. They are probably the most important weapons in our debugging arsenal.
For Fun
Put a breakpoint or single-step through to the "Loop While ..." line. I asked earlier, "Could it be useful to find out what values are in "Answer1" and "Answer2" at this point?". Here's how you can find out... 1) Press F6. You'll notice that the word "Immediate" towards the bottom of the screen becomes highlighted. If you didn't notice it, press F6 a few more times, and you'll notice that each time you press F6, one or other of "Untitled" and "Immediate" get highlighted, and the other un-highlighted. We call these "panes". So we might say "the code pane" or "the untitled pane" (both referring to the section entitled "Untitled"), or we might say "the immediate pane". Sometimes, if we're being a bit lazy, we might say "the code window" or "the untitled window" or particularly "the immediate window", but that's probably not the most technically accurate description of these sections of QBasic. (It is accurate for other IDEs, but let's not totally drown you in details!!! :o) ) So back to where we were - use F6 to make the immediate pane active. (You'll know it's active 'cause 1) the word "Immediate" will be highlighted; and 2) the keyboard input cursor will be somewhere in the blue box under the word "Immediate".) Phew! We got there. Now, type "Print Answer1" and press Enter. Whoa! What happened? The output screen came up again, and whatever we typed in as an answer to the first question has appeared on the screen again! Press any key and you get back to the IDE. Do you understand what happened? Any instruction that you type into the "Immediate" pane, if you press Enter, will be run immediately! ("Aha! Now I understand why they call it that!" :o) )
Here's another thing to try : instead of having "Do" on a line by itself and "Loop" with a lot of extra stuff after it, try moving that extra stuff from the "Loop" line to the "Do" line. (I've demonstrated this below in case you get stuck.) Run the program. Look at the source code. Did this change make any difference? Why or why not? (I'd like to hear your thoughts on this one, for those who are new to loops. You can email me directly or email the group.)
Do While Answer1 <> "let me in!" Or Answer2 <> "ditto that!"
Line Input "What is the first password? ", Answer1
Line Input "What is the second password? ", Answer2
Loop
Coming Soon...
We looked briefly in this lesson at the most flexible kind of loop that QBasic offers, but QBasic actually offers several more types of loops, and there are all sorts of useful things you can do with them. We're also going to start looking at rudimentary graphics programming in QBasic.