QBasic Tutorial 4 - Variables and Data Types - QB64


Data Types in QBasic / QB64
The computer can hold data in memory. The programmer must tell the computer what type of data to hold. This is called a data type.

String.........Text and characters
Example: This line is an example of a string

Integer.......Non-floating-point numbers from -32,768 to 32,767
Examples: 67, -34, -100, 203, 1022, -1, 0

Long..........Non-floating-point numbers from -2,147,483,648 to 2,147,483,647
Examples: 560005, 3, -2, 0, -867000, 14, 8, -10

Single........Floating-point numbers from -3.37x10^38 to 3.37x10^38
Examples: 4.3, 25.4567, -35.87, 0.35, -3.14

Double......Floating-point numbers from -1.67x10^308 to 1.67x10^308
Examples: 745663.90596, -98.12, 4859903.094491

Note: In QBasic 1.1, the Double may not work properly on some computers.

64 Bit Data Types (QB64 Only)

_INTEGER64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

_FLOAT ±1.18E−4932, ±1.18E+4932

List of QB64 Data Types
qb64.com/wiki/Variable-Types


Variables
• Hold data in memory
• Are assigned a data type
• The data can change throughout the program’s operation
• The data entered must be the same data type as assigned to the variable

A variable name is a name that is given to the data. The name must not start with a number or character that is not a letter. Also, the name of the variable must not be a reserved name like PRINT, INPUT, LET, ABS, BEEP, etc.

There are two ways to declare a variable in QBasic / QB64.

The first is to put a data type symbol after the name
$ String
% Integer
& Long
! Single
# Double
&& _INTEGER64 (QB64 Only)
## _FLOAT

Examples:

MyName$
Num1%
Num2!
Answer!

The second way is the preferred way since Visual Basic uses this method. Becoming accustom to this way will help the transition from QBasic / QB64 to Visual Basic. DIM is used to make variables of a data type.

DIM [Variable Name] As Data Type
DIM [Variable Name] AS STRING
DIM [Variable Name] AS INTEGER
DIM [Variable Name] AS LONG
DIM [Variable Name] AS SINGLE
DIM [Variable Name] AS DOUBLE
DIM [Variable Name] AS _INTEGER64
DIM [Variable Name] AS _FLOAT

Examples:

DIM MyName AS STRING
DIM Num1 AS INTEGER
DIM Num2 AS SINGLE
DIM Answer AS Double

Remember that selecting the right data type for the variable is very important to make the program operate properly. 



QBasic / QB64 Links

QB64 is a free download at www.qb64.com

QB64 Wiki & Manual is found at qb64.com/wiki.html

QBasic 1.1 comes with OldDOS. A zip file of OldDOS can be found at www.pcxt-micro.com/download.html

QBasic 1.1 needs DOSBox to run on Windows Vista and greater, Mac, and Linux.
DosBox is a free download at www.dosbox.com

Free QB64/QBasic Code

QBasic.Net - www.qbasic.net

Pete's QBasic Site - www.petesqbsite.com/index.php

QB45 - qb45.org

Phatcode - games.phatcode.net