BASIC Programming/Beginning BASIC/Variables and Data Types

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Variables allow you to store and change information. Below is an example how to use variables.

[edit] Example Code for Variables

Example 1 (qBasic)

CLS
ASTRING$ = "Hello World"
ANUMBER% = 10
PRINT ASTRING$
PRINT ANUMBER%

Example 2 (freeBasic)

Cls
Dim aString As String
Dim anInteger As Integer
aString = "John Doe"
anInteger = 42
Print aString
Print anInteger
Sleep
End

[edit] Output

Example 1

Hello World
10

In Basic, a string ends in a $, and whole numbers, known as integers, end with a %.

Example 2

John Doe
42

If you use Dim varName As DataType to declare variables, you do not need to use a suffix.

[edit] Arrays

An array is a collection of values.

Cls
Dim aNames(3) as String
aNames(1)="John Doe"
aNames(2)="Jane Doe"
PRINT aNames(1)
PRINT aNames(2)




Previous: PRINT, CLS, and END Main: Programming: BASIC Next: User Input