QBASIC - HANDOUTVARIABLES
A variable, simply defined, is a name which can contain a value. Programming involves
giving values to these names and presenting them in some form to the user.
A variable has a type which is defined by the kind of value it holds.
If the variable holds a number, it may be of integer, floating decimal, long integer,
or imaginary. If the variable holds symbols or text, it may be a character variable or
a string variable. These are terms you will become accustomed to as
you continue programming.
Here are some examples of values a variable might contain:
STRING "hello, this is a string"
INTEGER 5
LONG 92883
SINGLE 39.2932
DOUBLE 983288.18
The first is a string. Strings contain text. The last four are number types.
But the computer does not know what kind of value you are trying to give a variable
unless you tell it! There are two methods of telling the computer what kind of variable
you are using:
1. Explicitly declare the variable AS a type. This is done by using the DIM statement.
Say you wanted to make a variable called number which would contain an integer
(whole number, no digits after the decimal point). You would do it like this:
DIM number AS INTEGER
Then you would use that variable as an integer. The word DIM actually originates
from the word Dimension, but you won't see why until we discuss the topic of arrays.
2. Put a symbol after the variable name which is defined as representing that type.
QBasic has a set of symbols which represent each variable type:
$ String
% Integer
& Long
! Single
# Double
Appending one of these symbols to the name of a variable when you use it in the
program tells the computer that you are using it as that type. This topic of
data types is actually a difficult concept to grasp for newcomers to programming.
The most common error in QBasic is the infamous Type Mismatch which you will see a lot.
This means that you are trying to put a value into a variable of the wrong type.
You might be trying to put the letters "hi there" into an integer variable.
If you don't define the type of the variable,
then QBasic assumes it is of the Single type, which can often yield unexpected results.
I personally prefer to use the type symbols after variable names, but some explicitly
declare them usually at the head of their programs
The IF and THEN commands
The IF and THEN commands are used to compare an expression and then perform some task based on that expression.
x = 5
IF x = 5 THEN PRINT "x equals 5"
Since X does equal 5 in this case, the program outputs:
x equals 5
Expression signs
You can also enter the following statements, instead of the equals sign:
x < 5 (x is less than 5)
x > 5 (x is greater than 5)
Run the following:
x = 16
IF (x > 5) THEN PRINT "x is greater than 5"
Output:
x is greater than 5
You can also combine the signs like this:
x <= 5 (x is less than or equal to 5)
x >= 5 (x is greater than or equal to 5)
x <> 5 (x does not equal 5)
Run the following example:
CLS
x = 5
IF (x >= 5) THEN PRINT "x is greater than or equal to 5"
IF (x <= 5) THEN PRINT "x is less than or equal to 5"
IF (x <> 5) THEN PRINT "x does not equal 5"
Output:
x is greater than or equal to 5
x is less than or equal to 5
ELSE
Using the ELSE command, you can have the program perform a different action if the statement is false.
x = 3
IF x = 5 THEN PRINT "Yes" ELSE PRINT "No"
Since X doesn't equal 5, the output is:
No
END IF
END IF allows you to have multiple commands after the IF...THEN statement, but they must start on the line after the IF statement. END IF should appear right after the list of commands.
x = 5
IF (x = 5) THEN
x = 16 TIP: There is a way to have multiple commands
after IF...THEN without using END IF. To do so, place a
colon between each command. IF (x = 5) THEN INPUT a$: PRINT a$ |
INPUT a$
PRINT a$
END IF
The following program uses ELSE with the END IF command:
INPUT a$
PRINT a$
ELSE
PRINT x * 2
END IF
Output:
32
ELSEIF
The ELSEIF command allows you to perform a secondary action if the first expression was false. Unlike ELSE, this task is only performed if a specified statement is true.
x = 6
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
END IF
Output:
Statement 2 is true
You can have multiple ELSEIF commands, along with ELSE.
x = 8
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
ELSEIF (x = 7) THEN
PRINT "Statement 3 is true"
ELSE
PRINT "No above statements are true"
END IF
Output:
No above statements are true
Multiple expressions
You can have more than one expression in IF...THEN by using either the OR operator or the AND operator.
The OR operator only requires one expression to be true in order to print "Yes" in the following program:
x = 20
IF (x = 5 OR x = 20) THEN PRINT "Yes"
Output:
Yes
The AND operator requires both expressions to be true.
x = 7
IF (x > 5 AND x < 10) THEN PRINT "True"
Output:
True
This is a slightly more complex example:
x = 16
y = 3
IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT "Correct"
Output (since Y is 3):
Correct
Strings in IF...THEN
So far in this chapter, we've only been dealing with numbers, but you can also use strings with the IF...THEN command.
x$ = "Hello"
IF (x$ = "Hello" OR x$ = "World") THEN PRINT x$
Output:
Hello
You can also compare two variable strings:
x$ = "Hello"
y$ = "World"
IF (x$ <> y$) THEN PRINT x$; " "; y$
Output:
Hello World
Labels and the GOTO and GOSUB commands
The GOTO and GOSUB commands enables you to jump to certain positions in your program. Labels are used to specify what point in the program to continue execution.
GOTO
To use GOTO, place a label somewhere in your program, and then enter.
GOTO <label>
Run the following example program:
PRINT "1"
GOTO TheLabel
PRINT "2"
TheLabel:
PRINT "3"
Output (notice how PRINT "2" is skipped):
1
3
GOSUB
The GOSUB command is the same as GOTO, except when it encounters a RETURN statement, the program "returns" back to the GOSUB command. In other words, RETURN continues program execution immediately after the previous GOSUB statement.
PRINT "1" TheLabel: (Note: The END command exits
the program.) | Since the program returns to
the GOSUB command, the number 2 is
printed this time. 1 |
Line numbers
"Line numbers" can be used as labels.
PRINT "1"
GOTO 10
PRINT "2"
10 PRINT "3" (Notice the line number)
You can also write the program like this:
10 PRINT "1"
20 GOTO 40
30 PRINT "2"
40 PRINT "3"
The line numbers don't even have to be in sequence.
17 PRINT "1"
2 GOTO 160
701 PRINT "2"
160 PRINT "3"
Each of these programs output:
1
3
Guessing game
The following is a simple guessing game:
CLS
start:
PRINT "Guess a number between 1 and 10: ";
INPUT num
IF (num < 1 OR num > 10) THEN
PRINT "That is not between 1 and 10"
GOTO start
END IF
IF (num = 6) THEN
PRINT "Correct!!!"
ELSE
PRINT "Try again"
GOTO start
END IF
Output (may be slightly different):
TIP: Notice the
second PRINT statement
under PRINT "Try again". It adds a blank line
under Try again when the program is executed. |
Guess a number between 1 and 10: ? 2
Try again
Guess a number between 1 and 10: ? 7
Try again
Guess a number between 1 and 10: ? 6
Correct!!!
Loops
"Loops" make it easier to do an action multiple times. There are at least four types of loops: IF...GOTO, WHILE...WEND, DO...LOOP, and FOR...NEXT.
IF...GOTO
This program uses IF...GOTO to create a loop:
x = 10 | Output: 10 |
WHILE...WEND
The WHILE...WEND commands continue a loop until a specified expression is false.
To use WHILE...WEND:
1. Place an expression after WHILE
2. Enter a list of commands
3. Place WEND at the end
Run the following:
x = 10 PRINT x WEND | Output (same as in previous
example): 10 |
DO...LOOP
DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight advantages. With DO...LOOP you can:
1. Loop until an expression is true
2. Loop at least one time regardless of whether the expression is true or not.
To use DO...LOOP:
1. Specify whether the loop continues "while" the expression is true or "until" the expression is true, using the WHILE and UNTIL statements, respectively.
2. Place an expression after WHILE/UNTIL
3. Enter a list of commands
4. Place LOOP at the end
The following uses the WHILE statement:
x = 10
DO WHILE x < 15
PRINT x
x = x + 1
LOOP
This program uses the UNTIL statement:
x = 10
DO UNTIL x = 15
PRINT x
x = x + 1
LOOP
They both output:
10
11
12
13
14
If you place the expression at the end of the loop instead, the program goes through the loop at least once.
x = 32
DO
PRINT x
x = x + 1
LOOP WHILE x < 5
This is the output because the loop was only gone through one time:
32
FOR...NEXT
FOR...NEXT provides an easier way to create a loop.
FOR x = 1 TO 5
PRINT x
NEXT x
|
Output:
1
2
3
4
5
Also, you can use the STEP attribute to specify how much X will be increased each time through the loop.
FOR x = 1 TO 5 STEP 2
PRINT x
NEXT x
Output:
1
3
5
STOPPING LOOPS
To stop a loop prematurely, use the EXIT command, followed by either FOR or DO.
FOR x = 1 TO 5
PRINT x
IF (x = 3) THEN EXIT FOR
NEXT x
Output:
1
2
3
(NOTE: This command only works with the DO...LOOP and FOR...NEXT commands, not with WHILE...WEND or IF...GOTO.)
No comments:
Post a Comment