BASIC Programming/Beginning BASIC/Variables and Data Types

From Wikibooks, open books for an open world
Jump to: navigation, search

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

Contents

Example Code for Variables [edit]

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

Output [edit]

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.

Arrays [edit]

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)

Output [edit]

John Doe
Jane Doe
Previous: Beginning BASIC/PRINT, CLS, and END Index Next: Beginning BASIC/User Input