| QBASIC Commands and Instructions |
|
An important component of this course, along with the problem sets and discussions, is some very rudimentary computer programming that will give us insight into mathematical concepts and also teach us some basics of computer technology. The following are a set of QBASIC commands and instructions that are useful for the programming assignments in MA17. There is a more complete list in the MA17 Manual, but for the programming that we will be doing in this on-line version of the course, all you will need are the programming elements given here. Remember that this is not a programming course, but rather a course in computer math concepts that uses rudimentary programming to illustrate concepts in math and computers, and the relationship between the two. Use this page to begin learning the QBASIC programming language. Print out this short user's manual and type in all of the sample programs I have included in the descriptions below. I have used a courier (non-propotionally spaced) font for this page. That is so that the programming examples will look the same here as when you type them into the QBASIC window on your computer. QBASIC runs in a DOS window under Microsoft Windows. If you are using a Macintosh computer, as I am, then you cannot run QBASIC unless you have Virtual PC on your machine. If you have a Mac and you don't know what I am talking about here, you must contact me immediately. To get QBASIC installed on your system you must download a compressed file (so-called "zip" file), and then decompress it (unzip it) on your disk. If you have an unzip program on your system, it is straightforward to download the zip file using the link below, and then douple-clicking on the downloaded folder to decompress the files contained in it. If you don't have an unzip program installed on your system, you will have to download and install that first. The link for downloading the complete QBASIC system is The link for downloading the unzip program is Once you have downloaded and installed QBASIC, try typing the sample programs given below into QBASIC, and executing them. If you have difficulties, let me know and I will try to help you. You can also get help from any of the campus math skills or academic computing centers. Following you will find the QBASIC reference material that you need for this course. Please let me know if you have any corrections or suggested additions or improvements. Exponentiation (^)
This symbol (^), called a carot, is on the same key as the number six on your keyboard. The exponentiation operator allows you to raise numbers to a power. For example x = y^2 will take the contents of location y, square it and store the result in location x. Try the following simple program: CLS Multiplication (*)
This symbol (*), called an asterisk, is on the same key as the number eight on your keyboard. The mulitplication operator allows you to multiply two numbers. Try the following simple QBASIC program: CLS Division (/)
This symbol (/), the forward slash, is on the same key as the question mark. The division operator allows you to divide two numbers. Try the following simple QBASIC program: CLSTry this program again, only set Y = 0 instead of Y = 10 and see what happens. Addition (+)
The plus sign (+) is on the same key as the equal sign. The addition operator allows you to add two numbers. Try the following simple QBASIC program: CLS Subtraction (-)
The minus sign (-) is on the key next to the plus sign. The subtraction operator allows you to subtract two numbers. Try the following simple QBASIC program: CLS FUNCTIONS ABS( )
Example: X = ABS (25-65) The location corresponding to X will contain the number 40. Try the following simple QBASIC program: CLS SQR( )
Example: X = SQR(25) The memory location corresponding to X will contain the number 5. Try the following simple QBASIC program: CLS Try this program a few times with different values assigned to the variable X. Try, for example, X = -25 and see what happens. LOGICAL OPERATORS The AND operator does the same job as the logical "and" connective from mathematical logic. The logical expression (condition1 AND condition2) is true only if both condition 1 and condition 2 are true. Example: (1 < 5) AND (6 < 7) is true ORLogical "or" The logical expression (condition 1 OR condition 2) is true only if either condition1 or condition2 are true, or if both condition 1 and condition 2 are true. Example: (1 < 2) OR (7 > 8) is true STATEMENTS CLSClear Screen DIM Declares an array. The statement DIM arrayName(upper) declares an array with subscripts ranging from 0 to upper, where upper is an integer. Example: DIM xarry(30) defines an array that is 1 by 30 words long. A statement DIM arrayName(lower, upper) declares a doubly subscripted, or two-dimensional, array. Example: yarray(6,10) defines an rectangular array with 6 rows and 10 columns. END End a program, procedure, block, or user-defined data type. INPUT Allows program user to input numbers or character strings to a program. The statement INPUT var causes the computer to display question mark and to pause until the user enters a response. This response is then assigned to the variable var. A statement of the form INPUT "prompt"; var inserts a prompting message before the question mark. Example: INPUT "Enter your name"; name$ PRINTThis instruction writes data to the screen . PRINT expressionlist (list items separated by comma or semi-colon) Example: PRINT "This is a test";" only a test" REMRemark, this allows comments to be inserted in a program (the instruction is not executed). The REM command can be replace by an apostrophe. Example: REM The next six lines of code get input from the user. ‘The next six lines of code get input from the user. LOOP STRUCTURES Pre-test Loop DO WHILE condition .... This loop structure does the testing at the beginning of the loop, if the condition is met, then the statements within the loop will be executed. The repetition will stop once the condition is no longer true. Consider the following QBASIC Program: CLS Post-test Loop DO This loop structure does the testing at the end of the loop, if the condition is satisfied, then the loop is terminated, otherwise, loop continues. Consider the following QBASIC Program: CLS Loop that is executed a preset number of times FOR variable = initial TO limit STEP increment In this structure, variable serves as the counter. It is initialized to the number specified by initial, incremented by the number specified after the word STEP, and ends the loop when variable is equal to or more than what is specified by limit. If STEP is not specified, the default is the counter will be incremented by 1. STEP could also decrement instead of increment. Consider the following QBASIC program: CLS DECISION STRUCTURES IF statement IF condition THEN action A statement of the above form causes the program to take the specific action if condition is true. Otherwise, execution continues at the next line. Consider the following QBASIC program: CLS This program gives the basic structure for providing inputs to a program and using them to do calculations and then printing out answers. It is a structure that will be used in almost all of the programs that we will create in this course. IF THEN ELSE IF condition THEN action1 ELSE action2 A statement of the above form causes the program to take action1 if condition is true and action2 if condition is false. BLOCK IF IF condition1 THEN A block of statements such as the one above indicates that the group of statements between IF and END IF are to be executed only when condition is true. If the group of statements is separated into two parts by an ELSE statement, then the first part will be executed if the condition is true and the second part will be executed when the condition is false. Gonsider the following QBASIC program containing a block if statment: CLS |