Qbasic/Full Book View
Total Page:16
File Type:pdf, Size:1020Kb
QBasic/Full Book View < QBasic This page may need to be reviewed for quality. Contents [hide] 1 Text Output 1.1 Print 1.2 1HELLO.BAS 1.3 Understanding 1HELLO.BAS 1.4 2HELLO.BAS 1.5 Commas and Semicolons in PRINT and CLS 1.6 3HELLO.BAS 1.7 Variables 1.8 4FACE.BAS 1.9 Locate statement 1.10 5FACE.BAS 1.11 Colors 1.12 Color by Number 1.13 Summary 2 Basic Input 2.1 6INPUT.BAS 3 Basic Math 3.1 Equation Setup 3.2 7MATH.BAS 3.3 Understanding 7MATH.BAS 3.4 8MATH.BAS 3.5 Understanding 8MATH.BAS 3.6 9TIP.BAS 3.7 Tip Calculator 3.8 10OROP.BAS 3.9 Parentheses and Order of Operations 3.10 Random Numbers 3.11 11RND.BAS 3.12 More on RND 4 Flow Control 4.1 Conditional execution 4.2 True or False 4.3 IF 4.4 IF...THEN...ELSE 4.5 13 ELSEIF 4.6 FOR...NEXT 4.7 14FOR.BAS 4.8 WHILE...WEND 4.9 15WHILE.BAS 4.10 DO...LOOP 4.11 12IF.BAS 4.12 SELECT CASE 5 Advanced Input 5.1 INKEY$ 5.2 The keyboard buffer 5.3 Scancodes 6 Subroutines and Functions 6.1 Purpose 6.2 Procedure vs. Function 6.3 GOTO and GOSUB 6.4 ON ERROR 6.5 Declaring a subroutine 6.6 Declaring a function 7 Arrays and Types 7.1 Built-in Types 7.2 User-defined type 7.3 Array 7.4 Multidimensional array 7.5 Non-zero base 8 Appendix 8.1 Commands 8.1.1 ABS() 8.1.2 ACCESS 8.1.3 ASC() 8.1.4 ATN() 8.1.5 BEEP 8.1.6 CASE 8.1.7 CHAIN 8.1.8 CHDIR 8.1.9 CHR$() 8.1.10 CINT() 8.1.11 CIRCLE 8.1.12 CLEAR 8.1.13 CLOSE 8.1.14 CLS 8.1.15 COLOR 8.1.16 COMMON 8.1.17 CONST 8.1.18 DATA 8.1.19 DATE$ 8.1.20 DIM 8.1.21 DO .. LOOP 8.1.22 DRAW 8.1.23 END 8.1.24 END IF 8.1.25 ENVIRON 8.1.26 EOF() 8.1.27 ERASE 8.1.28 ERROR 8.1.29 EXIT 8.1.30 FOR .. NEXT 8.1.31 GOSUB 8.1.32 IF 8.1.33 INCLUDE 8.1.34 INKEY$ 8.1.35 INPUT 8.1.36 INPUT # 8.1.37 INSTR 8.1.38 LEFT$() 8.1.39 LET 8.1.40 LINE 8.1.41 LINE INPUT # 8.1.42 LOADIMAGE 8.1.43 LOOP 8.1.44 LPRINT 8.1.45 MID$ 8.1.46 OPEN 8.1.47 PALETTE 8.1.48 READ 8.1.49 REM or ' 8.1.50 RETURN 8.1.51 PLAY 8.1.52 PRINT 8.1.53 PSET 8.1.54 SCREEN 8.1.55 SEEK 8.1.56 SGN 8.1.57 SLEEP 8.1.58 SOUND 8.1.59 STR$ 8.1.60 SYSTEM 8.1.61 THEN 8.1.62 TO 8.1.63 USING 8.1.64 VAL() 8.1.65 WHILE ... WEND 9 Authors Text Output [edit] Text Output Print [edit] The text output command is PRINT. This is the command that we will be exploring through this section. PRINT takes a series of arguments. What that means is that in QBasic, after typing PRINT (note, for a short cut, just type a question mark '?') a programmer types the things he wants displayed and PRINT will display them. The first program will display the words "Hello World" on the screen. PRINT [Text to screen] 1HELLO.BAS [edit] PRINT "Hello World" Understanding 1HELLO.BAS [edit] First, copy everything (in this case, one line, but later programs will be much longer) into a text editor or into QBasic itself and save it as '1HELLO.BAS'. Next open it in QBasic (if you are not already there) and press F5. This will run the program. A black screen with the words "Hello World" in white on your left hand side of the screen should appear. This is your first QBasic program. Now, Press F5 again. Now there should be 2 lines saying "Hello World". This is because QBasic does not clear the screen every time a program is run. To change this we will use the command CLS, which stands for CLear Screen. We will be looking at some other new concepts in the next program as well. 2HELLO.BAS [edit] PRINT "This line will be erased" CLS PRINT "Hello"; PRINT " World", PRINT "Hello Jupiter" PRINT "Good Bye",,"For";" Now" PRINT 1,2,3,4,5 Commas and Semicolons in PRINT and CLS [edit] Here is what the program output should look like: Hello World Hello Jupiter Good Bye For Now 1 2 3 4 5 Addressing 2HELLO.BAS in line order, the first line prints "This line will be erased." to the screen, but in the second line, the CLS command clears the screen immediately after, so it will not be seen (technically it flashes at the bottom form columns). "Hello Jupiter" should line up with '2' at the bottom. More than one comma can be used in a row. In this example, after "Good Bye" two commas are used to move "For Now" over two tab columns. "For Now" should line up with '3'. My final statement on this topic is to play around with it. Try using commas and semicolons in a program. 3HELLO.BAS [edit] CLS hello$ = "Hello World" number = 12 PRINT hello$, number Variables [edit] Variables are used to store information. They are like containers. You can put information in them and later change the information to something else. In this first example they may not seem very useful but in the next section (Input) they will become very useful. In this example we use two types of variables - string variables and numeric variables. A string variable holds words, a string of characters (a character is a number, letter or symbol). In this case the characters are letters. A string variable is denoted by ending the name of the variable with a dollar sign. The string variable in this program is hello$. What ever you set hello$ equal to will be displayed in the PRINT statement. The numeric variable is number. Numeric variables do not have a special ending like string variables. 4FACE.BAS [edit] CLS LOCATE 14, 34 'position the left eye PRINT "<=>" 'draw the left eye LOCATE 14, 43 'position the right eye PRINT "<=>" 'draw the right eye LOCATE 16, 39 'position the nose PRINT "o|o" 'draw the nose LOCATE 18, 36 'position the mouth PRINT "\_______/" 'draw the mouth LOCATE 19, 42 'the bottom PRINT "The Face by QBasic" Locate statement [edit] Locate allows you to position the cursor for the next piece of text output. Contrary to cartesian coordinates which read (X,Y), the locate statement is LOCATE Y,X. In this case Y is the distance down from the top of the screen and X is the distance from the left side of the screen. The reason that LOCATE does not follow the standard coordinate system is that it is not necessary to include the X portion, you can use the format LOCATE Y which just specifies the line to start on. 5FACE.BAS [edit] CLS LOCATE 14, 34 COLOR 9 PRINT "<=>" LOCATE 14, 43 PRINT "<=>" COLOR 11 LOCATE 16, 39 PRINT "o|o" COLOR 4 LOCATE 18, 36 PRINT "\_______/" COLOR 20 LOCATE 19, 42 PRINT "U" LOCATE 1, 1 COLOR 16, 1 PRINT "Hello World" Colors [edit] The program 5FACE.BAS is broken into sections to make it easier to read. This is an example of a good programming habit. Each three line piece of code each piece of code specifies what color it's part of the face should be, where it should be and what it should look like. The order of the position and the color is unimportant. The new statement COLOR allows you to change the color of the text. Once changed, all output will be in the new color until COLOR or CLS is used. The format for the COLOR statement is: COLOR [foreground] or COLOR [foreground],[background] The colors are designated by numbers which will be discussed in the next section. Color by Number [edit] There are 16 colors, numbered from 0 to 15. 0 Black 8 Dark Grey (Light Black) 1 Blue 9 Light Blue 2 Green 10 Light Green 3 Cyan 11 Light Cyan 4 Red 12 Light Red 5 Purple 13 Light Purple 6 Brown/Orange 14 Yellow (Light Orange) 7 Light Grey (White) 15 White (Light White) If you look carefully at this chart you can see that there are 8 main colors (0 through 7) and then those colors repeat, each in a lighter shade. You may also notice that the colours act as a combination of binary values (where blue=1, green=2, red=4, etc.) This makes it much easier to memorize the color scheme. Blinking colors are also available: at 16, the colors start over again with blinking black and extend through 31 (blinking white). However, the blinking option is not available for the background, only for the text (foreground). It is possible to switch the blinking foreground text with an intense background, but this task is beyond the scope of the QBasic textbook, and may not work when Windows displays the console in windowed mode. Summary [edit] In this section we looked at several methods to manipulate output.