1 QUICKBASIC SIMPLE VARIABLES Variables are used to ...

32 downloads 152 Views 32KB Size Report
You can't use QuickBasic key words as variables and your variable cannot begin with the letters ... A simple computer program consists of three main parts. 1.
QUICKBASIC SIMPLE VARIABLES Variables are used to represent both numeric and character string data. A variable does not have to be a single letter, but can be up to 40 characters long. You can't use QuickBasic key words as variables and your variable cannot begin with the letters FN. VARIABLE TYPES There are five types of simple variables, four numeric variables and one character string variable. Each is distinguished from the other by a symbol at the end of the variable. The five types are: 1.

Integer - positive and negative whole numbers and zero. Special symbol used is %. Examples:

LET X% = 15 LET WIDTH% = 34

The Range of this variable is APPROXIMATELY 32000 2.

Long Integer - same as integer, only it has a longer range. Special symbol is &. Examples:

LET X& = 100000 LET LIGHTSPEED& = 3E81

The range of this variable is 2 BILLION 3.

Single Precision - numbers with decimals (7 DIGITS). Special symbol is !. Examples:

LET PI! = 3.14159 LET ATOMRADIUS! = 1.02E-13

The Range of this variable is 3 x 1038 4.

Double Precision - same as single precision only 15 DIGITS. Special symbol is #. Examples:

LET ET# = 345654.3212343

The Range of this variable is 1.8 x 10308 5.

The computer can also use variables to represent letters or character strings. These variables are often called by the abbreviation, string variables. Special symbol is $. Examples:

NAME$ = "JOHN DOUGH" MESSAGE$ = "HOW'S SHE GOIN'"

Note: The character string is always enclosed in quotes after a LET statement. VARIABLE ASSIGNMENT 3E8 is Scientific Notation. In this case 3 x 10 8 or 300,000,000.

1

1

There are three main methods of assigning information to variables. 1.

LET Examples: see page 1 Note: The LET statement is optional. LET X% = 40 and X% = 40 are the same.

2.

INPUT Examples:

INPUT X% INPUT"PLEASE ENTER YOUR NAME", NAME%

Note: The second example contains a prompt which makes it easier to understand what the program is doing. 3.

READ .....DATA These are two separate statements that are used togather. It doesn't make any difference where the DATA statement is placed. Examples:

READ A%, B% PRINT A%, B% DATA 101, 345 READ FIRSTNAME$, SECONDNAME$ DATA JOHN, DOUGH PRINT SECONDNAME$; FIRSTNAME$

2

A simple computer program consists of three main parts. 1. 2. 3.

The input of data into the machine. The processing of the data in some way. The display of the processed data in some form, usually on the screen or on a printer.

The input of data is usually done using the three variable assignment statements already discussed. The processing of data can take the form of a formula, one of QuickBasics built in functions or other special techniques. Where formulae are involved the math operators include: + addition, - subtraction, * multiplication, / division, ^ raise to a power There are also two other special operators that we will see again later. \ integer division and mod remainer in division All formulae follow the normal rules for order of operations. EXAMPLE:

CLS INPUT"ENTER EMPLOYEE NAME", NAME$ INPUT "ENTER HOURLY RATE", HRRATE! INPUT"ENTER HOURS WORKED", TIME! INPUT'INCOME TAX RATE", RATE! LET GROSSPAY! = HRRATE! * TIME! LET RIPOFF! = GROSSPAY! * RATE! / 100 LET TAKEHOME! = GROSSPAY! - RIPOFF!

| | Input section | | | | Process section |

CLS PRINT "Employee : "; NAME$ PRINT PRINT "Gross pay "; GROSSPAY! PRINT PRINT "Income tax"; RIPOFF! PRINT PRINT "Net pay "; TAKEHOME!

| | | | Output section | | |

3

Computers can also make decisions based on simple rules. To make choices QuickBasic uses the relational operators. Some of these are: > < = >=