![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Home | ||||||
Welcome to The QBSpot! | ||||||
This is the first-ever QBasic tutorial written by High Text Computing. Please enjoy! For more information, e-mail qbspot@zwallet.com. The Basics What exactly are the "Basics" of Programming, in general? Well, what's the first thing you want to learn how to do in QBasic? You want to learn how to put some text on the screen, right? That's IO - Input/Output. Actually, for now it's just Output. Here's how you will do that: 'Firstqb.bas - First QBasic Program, showing how to Output to the screen in QB. 'This is a comment. REM This is a comment, or REMark, to. PRINT "Hello, World!" Notice the PRINT statement. Also notice the comments or remarks. This is just a BASIC (pardon the pun) representation of BASIC I/O in QBasic. Now you know how to do Input in QBasic. That's easy: 'QBProg2.bas - I/O in QBasic PRINT "Enter a number:" INPUT num1 'The other way of doing INPUT: INPUT "Enter another number: "; num2\ 'Now add the two together: LET result = num1 + num2 'Actually, you can leave that LET out usually: result = num1 + num2 'Now put the result on the screen: PRINT num1; " + "; num2; " = "; result Notice the way you do math. Here are your math operators: + Plus, Add, Addition - Minus, Subtract, Subtraction * Times, Multiply, Multiplication / Divided by, Divide, Division ^ To the power of, exponent, exponentiation You can, in fact, print a mathematical result on the screen without using an extra variable! Here's how: PRINT 2 + 1 - 3 * 4 / 5 ^ 6 and the output will be: 0 Because (2 + 1) = (3) - 3 = (0) * 4 = (0) and 0 to the 6th Power = 0 But let's do something better than that: PRINT 2^8 this produces the result: 256 Does this remind you of our Binary Tutorial? Anyhow, that's the Basics of I/O in QBasic. Boy, have you learned a lot. One thing you Pro's might have noticed is that you don't have to declare variables, right? You can declare variables (and you should): DIM num1, num2, num3 or: DIM num1 AS INTEGER But you can break every rule of Programming in QBasic... and that's why I don't like it. It's easy for beginners to learn, but if you use QBasic, you'll get in the habit of Programming poorly, because QBasic (and other Microsoft BASIC languages, such as Visual BASIC) makes it easy to do just that! But if you still want to learn the worlds simplest, easiest Language, continue! Just remember, beginners, using QB too much will make it almost impossible to learn any other language, especially a good one (and even more especially ASM). Your programs could start lookig lik mi spellig hear. Yu woodent wahnt thet, wood yuo? You see? You get in the habit of breaking every rule (and my sentences weren't so bad, either; in QBasic you can break EVERY rule: grammar, spelling, etc.) and it gets easier and easier... and it was almost impossible for me to learn a relatively simple language, Pascal. Don't go the way I went!!! So if you STILL want to learn QBasic, continue: Colors and Strings No, we're not getting into graphics yet. We're just doing textmode colors and strings. A string is a bunch of letters, up to 256 letters. All put together. QBasic strings end with a "$". Like this: mystring1$ = "This is my string it is cool QBasic is cool isn't it?" I know, it's super dorky, but that's just an example. You can also get the user to INPUT a string: PRINT mystring1$ PRINT "Enter up to 256 letters, numbers and whatnot:" INPUT mystring2$ PRINT "This is what you entered: "; mystring2$ So what about colors, you ask? First you have to learn the MS-DOS Colors. If you read my General Programming Guide you don't need to read this, but you might want to refresh your memory: ---These colors (0-7) are accessible for foreground or background colors: 0 - Black 1 - (Dark) Blue 2 - (Dark) Green 3 - (Dark) Cyan 4 - (Dark) Red 5 - (Dark) Magenta 6 - (Dark) Brown 7 - (Light) Grey = "Dark" White ---These colors (8-15) are only accessible for foreground colors: 8 - (Dark) Grey = "Light" Black 9 - (Light) Blue 10 - (Light) Green 11 - (Light) Cyan 12 - (Light) Red 13 - (Light) Magenta 14 - (Light) Yellow 15 - (Bright) White These colors are almost universal throughout all DOS Programming Languages (C, C++, Pascal, QBasic, etc). So how do we use them? In QBasic: COLOR foregroundcolor, backgroundcolor and for those of you who are wondering, foregroundcolor is the text color. So here's my favorite combination of colors: COLOR 10, 1 'Bright Green Text, Dark Blue Background (The backgrounds cannot be any color between 8 'and 15 - if a backgroundcolor is greater than or equal to 8, it will get 8 subtracted, making it it's darker 'counter-color. PRINT "This text is green. The background is blue." Did you notice, after running this example, that only the background color BEHIND the text is blue - that the background on the rest of the screen is black? And how do we clear the screen anyways? I'll answer both questions with two words: use CLS. Example 1 - Clearing the screen: PRINT "You won't see this, because it'll be cleared right away." CLS PRINT "But you can see this text!" Example 2 - Making the entire background one color: COLOR 10, 1 CLS PRINT "The whole background is blue, unless you COLOR without CLS-ing again!" Here's the output: The whole background is blue, unless you COLOR without CLS-ing again! Notice the change in color! Cool, huh? So that's how you do color! Menus What is a menu? In windows, pressing ALT or F10 or clicking a button in the top of many windows brings down a little list of options which you can click. Well in QBasic, you can do all that kind of stuff, but right now we're just going to make a simple menu. Not a complicated one. Just one where you press a key to choose a menu option: Menu: CLS PRINT "(1)Menu 1" PRINT "(2)Menu 2" PRINT "(3)Menu 3" chosen = VAL(INKEY$) IF chosenmenu = 1 THEN CLS PRINT "Menu 1" PRINT "(1)--Item 1" PRINT "(2)--Item 1" PRINT "(3)--Item 3" PRINT "(4)--Close this menu" PRINT "Menu 2" PRINT "Menu 3" 'Turn a string (that the user entered) into a number chosen = VAL(INKEY$) IF chosen = 1 THEN 'Do something here. END IF IF chosen = 2 THEN 'Do something here. END IF IF chosen = 3 THEN 'Do something here END IF IF chosen = 4 THEN 'Close the menu, open the main menu. PRINT "(1)Menu 1" PRINT "(2)Menu 2" PRINT "(3)Menu 3" GOTO Menu END IF ELSEIF chosenmenu = 2 THEN 'Display Menu 2 ELSE 'Display Menu 3 END IF Now I'm just going to let you try and figure out exactlt what it does - I won't tell you. You figure it out! Just try it out and see! I think that's enough tutorial for now. Now it's time for review and test: (1)What does the menu program do? (2)How do you just get one character off the keyboard (and not more than one)? (3)How do you make the screen background color the same all over? (4)Make a program that gets a persons age and weight and decides if they're overweight or not for them. (5)Re-write your calculator Program to include menus, such as: (+)Add (-)Subtract (*)Multiply (/)Divide (^)Exponentiation Press the corresponding character to enter the two numbers. Send all answers, questions, programs, etc to qbspot@zwallet.com to be graded. You could win a prize! Thank you for reading High Text Computing's first QBasic Tutorial ever. Please send questions, comments, etc to qbspot@zwallet.com for more information. |