Wednesday, 2024-12-18, 7:59 PM

Welcome Guest | RSS

MainRegistrationLogin
Qbasic Home

Catalog categories
Qbasic Tutorial [31]

Rate my Site
Rate JPSor Site
Total of answers: 168

Main » Articles » Qbasic Tutorial

Chapter 26 - Qbasic Tutorial

Built-In Types

QBASIC offers five built-in types. Each of these types can be specified by a single character after the variable name. You can also specify a type using a DIM statement. It is important to pick the right types when you are writing a program. The following descriptions of each type will help you make the right decisions.

Single-Precision

The single-precision type handles numbers with decimals. You can go up to seven digits with a single-precision variable. In a DIM statement, use "SINGLE" to create a single-precision variable. The type-character for a single-precision variable is "!". Unless you do something special, any variable without a type character is single-precision. Here are some examples of creating and using single-precision variables:

 X = 1.5
DIM Y AS SINGLE
Y = 2.1
Z! = 2.5

PRINT X; Y; Z!

Notice that the DIM statement can be used to tell QBASIC the type of a variable. Then you don't need to use a type character for that variable.

String

The string type handles strings of characters. You cannot do math with string variables. In a DIM statement, use "STRING" to create a string variable. The type-character for a string variable is "$". Here are some examples of creating and using string variables:

 X$ = "Hello"
DIM Y AS STRING
Y = "Goodbye"

PRINT X$
PRINT Y

Integer

The integer type handles numbers without decimals. Integers may range from -32768 to 32767. Math with integers may be faster than math with single-precision variables. For programs that have to run very fast, using integers might be useful. In a DIM statement, use "INTEGER" to create an integer variable. The type-character for an integer variable is "%". Here are some examples of creating and using integer variables:

 X% = 32
DIM Y AS INTEGER
Y = 55

PRINT X%; Y

Since math with integers is very fast, you will commonly see the following line near the beginning of QBASIC programs:

 DEFINT A-Z

This tells QBASIC to stop assuming that every variable is single-precision, and instead to assume that all variables are integers. This way you don't need to use DIM or the "%" symbol throughout your program to make all your variables integers.

Long-Integer

The long-integer type handles numbers without decimals. Long-integers may range from -2147483648 to 2147483647. Math with long-integers is usually almost as fast as math with integers. For programs that have to run very fast, using long-integers might be useful. In a DIM statement, use "LONG" to create a long-integer variable. The type-character for a long-integer variable is "&". Here are some examples of creating and using long-integer variables:

 X& = 65536
DIM Y AS LONG
Y = 121072

PRINT X&; Y

Double-Precision

The double-precision type handles numbers with decimals. You can go up to fifteen digits with a double-precision variable. Double-precision variables are used where very accurate math is needed. In a DIM statement, use "DOUBLE" to create a double-precision variable. The type-character for a double-precision variable is "#". Here are some examples of creating and using double-precision variables:

 X# = 3.14159265358979
DIM Y AS DOUBLE
Y = 1.23456789012345

PRINT X#; Y

Arrays

An array lets you store a list of things. Arrays are very similar to variables, but they hold more than one thing. Try this:

 N$(0) = "Ted"
N$(1) = "Jack"
N$(2) = "Jill"
N$(3) = "Fred"

FOR I = 0 TO 3
PRINT N$(I)
NEXT I

The number inside the parenthesis "(1)" is called the "subscript". N$(0) is usually pronounced "N dollar sub zero", although I've also heard it called "N string sub zero".

Arrays can also store numbers.

 FOR I = 0 TO 10
A(I) = I * 2
NEXT I

FOR I = 0 TO 10
PRINT A(I)
NEXT I

Arrays are limited to holding only 11 items (0 through 10). If you go over 10, you'll get a "Subscript out of range" error. To make bigger arrays, you can use DIM to tell QBASIC how big the array will be:

 DIM A(20)

FOR I = 0 TO 20
A(I) = I * 2
NEXT I

FOR I = 0 TO 20
PRINT A(I)
NEXT I

Arrays are perfect for programs that need to keep a list of things. You could use arrays to make a phone book program, or a program that keeps track of the people in your class at school.

TYPE

Sometimes you'll want to put a bunch of different kinds of variables together because all together they describe something. QBASIC's TYPE statement lets you create your own collections of variables. Here's an example:

 TYPE FriendType
FullName AS STRING * 20
PhoneNumber AS STRING * 14
END TYPE

DIM Friend AS FriendType

Friend.FullName = "Joe Blow"
Friend.PhoneNumber = "1-310-555-1212"

PRINT Friend.FullName; ": "; Friend.PhoneNumber

TYPE makes our new type, or collection of variables. DIM makes a new variable of that type. When we work with types, we use the variable name, followed by a dot ("."), followed by the name of the variable in the TYPE.

Since TYPE lets you use a single variable to represent a collection of variables, you can use TYPE to pass many parameters to a SUB at once. This may be a good way to avoid using SHARED too much in a SUB.

In QBASIC, a TYPE can hold any of the built-in types. A TYPE can also hold another TYPE. However, a TYPE cannot hold an array. Visual BASIC allows that, but not QBASIC.

A Database

Using arrays and TYPEs together allows you to create what is known as a database. Try this:

 TYPE FriendType
FullName AS STRING * 20
PhoneNumber AS STRING * 14
END TYPE

' The database
DIM Friends(2) AS FriendType

' Fill the database with names and numbers
Friends(0).FullName = "Joe Blow"
Friends(0).PhoneNumber = "1-310-555-1212"

Friends(1).FullName = "Jack Sprat"
Friends(1).PhoneNumber = "1-340-555-6545"

Friends(2).FullName = "Carol Christmas"
Friends(2).PhoneNumber = "1-350-555-2421"

' Print out the entire database
FOR I = 0 TO 2
PRINT Friends(I).FullName; ": "; Friends(I).PhoneNumber
NEXT I


Source: http://jpsor.ucoz.com
Category: Qbasic Tutorial | Added by: JPSor (2009-02-26) | Author: JPSor
Views: 4168 | Rating: 5.0/1 |
Login

Qbasic Clock

Search

Visit my Friends

Who's Online

Total online: 1
Guests: 1
Resgistered: 0


Copyright MyCorp © 2024