Difference between revisions of "Variable"
imported>Cyperium (Created page with 'A '''variable''' is a "container" that can store a value, either numerical or string and it can later be changed by the program (as opposed to constants). All numeric…') |
imported>Clippy m |
||
Line 1: | Line 1: | ||
− | A '''variable''' is a "container" that can | + | A '''variable''' is a "container" name that can hold a numerical or string value which can be referenced or changed by the program (as opposed to [[CONST]]ant values which never change). |
− | + | <center>'''Variable names'''</center> | |
+ | Variables in QB64 can be any name except the names of keywords and may not contain spaces or non-alphabetical/non-numerical characters (except "." and "_"), if numerical characters are used in the name at least one alphabetical character has to be used before the numerical character, the same applies to ".", in case a "." is used in the name at least one character has to follow the "." (numerical or alphabetical). | ||
− | Variables | + | <center>'''Variable types'''</center> |
+ | Variables can be defined as a specific type using a variable type suffix or by using a [[DIM]] or [[REDIM]](for dynamic arrays only) statement [[AS]] a variable type of [[_BIT]], [[_BYTE]], [[INTEGER]], [[LONG]], [[SINGLE]], [[DOUBLE]], [[_INTEGER64]], [[_FLOAT]] or [[STRING]] in QB64. | ||
− | + | Groups of variable names can be type defined by the first letter or list of letters of the names using [[DEFINT]], [[DEFLNG]], [[DEFSNG]], [[DEFDBL]], [[DEFSTR]] or [[_DEFINE]] [[AS]] in QB64. | |
+ | |||
+ | |||
+ | <center>'''Variable values'''</center> | ||
+ | All numerical variables default to 0 and all string variables default to "" at the start of a program and when first referenced inside of a [[SUB]] or [[FUNCTION]] procedure except when the variable is defined as a [[STATIC]] value. | ||
+ | |||
+ | |||
+ | [[STATIC]] sub-procedure values keep their value after the procedure is exited. They will hold an empty value when first used. | ||
+ | |||
+ | |||
+ | Variables are used to hold program data information that is attained through the program flow, either by user input, calculations or by other ways of communicaton (as with I/O, memory, TCP/IP or files). | ||
+ | |||
− | + | Assignment of variable values can be done using the = assignment symbol (variable1.number = 500, for example). | |
− | [[Arrays]] is a special usage of variables that can hold many values in one variable by specifying a index enclosed in paranteses. | + | [[Arrays]] is a special usage of variables that can hold many values in one variable name by specifying a reference index enclosed in paranteses. |
Revision as of 19:06, 15 August 2011
A variable is a "container" name that can hold a numerical or string value which can be referenced or changed by the program (as opposed to CONSTant values which never change).
Variables in QB64 can be any name except the names of keywords and may not contain spaces or non-alphabetical/non-numerical characters (except "." and "_"), if numerical characters are used in the name at least one alphabetical character has to be used before the numerical character, the same applies to ".", in case a "." is used in the name at least one character has to follow the "." (numerical or alphabetical).
Variables can be defined as a specific type using a variable type suffix or by using a DIM or REDIM(for dynamic arrays only) statement AS a variable type of _BIT, _BYTE, INTEGER, LONG, SINGLE, DOUBLE, _INTEGER64, _FLOAT or STRING in QB64.
Groups of variable names can be type defined by the first letter or list of letters of the names using DEFINT, DEFLNG, DEFSNG, DEFDBL, DEFSTR or _DEFINE AS in QB64.
All numerical variables default to 0 and all string variables default to "" at the start of a program and when first referenced inside of a SUB or FUNCTION procedure except when the variable is defined as a STATIC value.
STATIC sub-procedure values keep their value after the procedure is exited. They will hold an empty value when first used.
Variables are used to hold program data information that is attained through the program flow, either by user input, calculations or by other ways of communicaton (as with I/O, memory, TCP/IP or files).
Assignment of variable values can be done using the = assignment symbol (variable1.number = 500, for example).
Arrays is a special usage of variables that can hold many values in one variable name by specifying a reference index enclosed in paranteses.
Examples
Example of different usages of variables:
max = 1000 DIM d(max) FOR c = 1 TO max d(c) = c + d(c - 1) NEXT PRINT "Show the result of the addition from 1 to n (1+2+3...+n)" PRINT "n = (0-" + LTRIM$(STR$(max)) + "): "; INPUT "", n IF n <= max AND n >= 0 THEN PRINT d(n) ELSE PRINT "Invalid value (only 0 to" + STR$(max) + " is permitted)."
Show the result of the addition from 1 to n (1+2+3...+n) n = (1-1000): 10 55
See also