Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

QBasic/Arrays and Types

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Built-in Types QBasic has five built-in types: INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#) float and STRING($). QB64 has two more built-in types: _INTEGER64 (&&) and _FLOAT (##) Implicit declaration is by adding the type character to the end of the variable name (%, &, !, #, $, &&, ##). See QBasic/Basic math for more. Explict declaration is by using the DIM statement before first use: DIM a AS STRING DIM b AS INTEGER DIM c AS LONG DIM d AS SINGLE DIM e AS DOUBLE DIM f AS _INTEGER64 'QB64 only DIM g AS _FLOAT 'QB64 only If you do not use either implicit or explicit declaration, QBASIC interpreter assumes SINGLE type. User-defined type A user defined type allows you to create your own data structures. Please note that custom types are similar to arrays. TYPE playertype name AS STRING score AS INTEGER END TYPE You can then declare variables under this type, and access them: DIM playername AS playertype playername.name = "Bob" playername.score = 92 This above example shows how a custom type can be used for maintaining data, say on a player who plays a game. Array An array is a collection of values stored in a single variable. A STRING is an array of characters (so, for example, char$(1) means 1st character in string char$). Arrays of numbers should be defined using the DIM instruction (unless you DIM them, they are limited to 10 elements on each dimension). By default, arrays in QBasic are static in size and cannot be changed later in the program. Code that will set up this type of array is as follows: DIM myArray(10) as TYPE 'this is explaining the datatype to be used during program execution in array' TYPE can be any of the built in QBasic (INTEGER, LONG, SINGLE, DOUBLE, STRING) or user-defined type. If this is not specified, the array takes the Type defined by the variable name suffix - INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#), STRING($) - or INTEGER if none. WARNING: If your data Type is string, DIM string(10) defines a SINGLE string of 10 characters, NOT 10 strings of arbitary length ! (10 strings of up to 128 chars each would be defined as DIM string(10,128) By issuing the Meta Command '$DYNAMIC at the beginning of your program you can cause your arrays to be dynamic: ' $DYNAMIC DIM myDynamicArray(5) as INTEGER REDIM myDynamicArray(10) as INTEGER This is now perfectly legal code. To free up space occupied by an array, use the ERASE statement. Multidimensional array An array isn't restricted to one dimension - it's possible to declare an array to accept two parameters in order to represent a grid of values. DIM housenames(25,25) as STRING You cannot use the REDIM statement to change the number of dimensions on the array, even with dynamic allocation. Non-zero base In most languages, arrays start at the value 0, and count up. In basic, it's possible to index arrays so that they start at any value, and finish at any other value. DIM deltas(-5 TO 5) You can change the default lower bound with the OPTION BASE statement.