Q(uick)BASIC Statement: CONST
Quick View
CONST
A non-executable statement that declares symbolic constants to use in place of numeric or string values
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- CONST constantname = expression [,constantname = expression]…
Description/Parameter(s)
constantname | The name of the constant. This name can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.). |
expression | An expression that is assigned to the constant. The expression can consist of literals (such as 1.0), other constants, any arithmetic or logical operators except exponentiation (^), or a single literal string. |
Example
CONST PI = 3.141593
INPUT "Radius of Circle: "; r
PRINT "Area = "; PI * r ^ 2
Syntax
- CONST constantname = expression [,constantname = expression]…
Description/Parameter(s)
constantname | A name following the same rules as a BASIC variable name. You may add to the name a type-declaration character (%, &, !, #, or $) to indicate its type, but this character is not part of the name. |
expression | An expression consisting of literals (such as 1.0), other constants, or any of the arithmetic and logical operators except exponentiation (^). You may also use a single literal string such as "Error on input". You cannot use string concatenation, variables, user-defined functions, or intrinsic functions like SIN or CHR$ in expressions assigned to constants. |
If you use a type-declaration character in the name, you may omit the character when the name is used, as shown in the following example:
- CONST MAXDIM% = 250
- ⋮
- DIM AccountNames$(MAXDIM)
If you omit the type-declaration character, the constant is given a type based on the expression in the CONST statement. Strings always yield a string constant. With numeric expressions, the expression is evaluated and the constant is given the simplest type that can represent the constant. For example, if the expression gives a result that can be represented as an integer, the constant is given an integer type.
Note: | Names of constants are not affected by DEFtype statements such as DEFINT. A constant's type is determined either by an explicit type-declaration character or by the type of the expression. |
Constants must be defined before they are referenced. The following example produces an error because the constant ONE is not defined before it is used to define TWO (constants are defined from left to right):
- CONST TWO = ONE + ONE, ONE = 1
Constants declared in a SUB or FUNCTION are local to the SUB or FUNCTION. A constant declared outside a procedure is defined throughout the module. You can use constants anywhere that you would use an expression.
A common programming practice is to use a statement like the following (recall that any nonzero value represents "true"):
- TRUE=-1
Constants offer several advantages over using variables for constant values:
- Constants need to be defined only once for an entire module.
- Constants cannot be inadvertently changed.
- In stand-alone programs, using constants produces more efficient code than using variables.
- Constants make programs easier to modify.
Example
This example uses the CONST statement to declare symbolic constants for the ASCII values of nonprinting characters such as tab and line feed. Constants also make programs easier to modify.
Tip: You must supply a text file when you run this example. Use a text file you have already created, create a file with a text editor, or specify the README.DOC text file.
' This program counts words, lines, and characters.
' A word is any sequence of nonblank characters.
DEFINT a-z
CONST BLANK = 32, ENDFILE = 26, CR = 13, LF = 10
CONST TABC = 9, YES = -1, NO = 0
CLS ' Clear screen
' Get the file name from the command line.
FileName$=COMMAND$
IF FileName$="" THEN
INPUT "Enter input file name: ",FileName$
IF FileName$="" THEN END
END IF
OPEN FileName$ FOR INPUT AS #1
Words=0
Lines=0
Characters=0
' Set a flag to indicate you're not looking at a
' word, then get the first character from the file.
InaWord=NO
DO UNTIL EOF(1)
C=ASC(INPUT$(1,#1))
Characters=Characters+1
IF C=BLANK or C=CR or C=LF or C=TABC THEN
' If the character is a blank, carriage return,
' line feed, or tab, you're not in a word.
IF C=CR THEN Lines=Lines+1
InaWord=NO
ELSEIF InaWord=NO THEN
' The character is a printing character,
' so this is the start of a word.
' Count the word and set the flag.
InaWord=YES
Words=Words+1
END IF
LOOP
PRINT Characters, Words, Lines
END
Syntax
- CONST constantname = expression [,constantname = expression]…
Description/Parameter(s)
constantname | A name that follows the same naming rules as a BASIC variable. You can add a type-declaration character (%, &, !, #, @, or $) to indicate its type, but this character is not part of the name. |
expression | Consists of literals, other constants, or any of the arithmetic and logical operators except exponentiation (^). |
- The expression can consist of literals (such as 1.0), other constants, or any of the arithmetic and logical operators except exponentiation (^). You also can use a single literal string such as "Error on input." You cannot use string concatenation, variables, user-defined functions, or intrinsic functions - such as SIN or CHR$ - in expressions assigned to constants.
Usage Notes
- If you use a type-declaration character in the name, you can omit the character when the name is used, as shown in the following example:
- CONST MAXDIM% = 250
- …
- DIM AccountNames$(MAXDIM)
- If you omit the type-declaration character, the constant is given a type based on the expression in the CONST statement. Strings always yield a string constant. With numeric expressions, the expression is evaluated and the constant is given the simplest type that can represent the constant. For example, if the expression gives a result that can be represented as an integer, the constant is given an integer type.
- Names of constants are not affected by DEFtype statements such as DEFINT. A constant's type is determined either by an explicit type-declaration character or by the type of the expression.
- Constants must be defined before they are referred to. The following example produces an error because the constant ONE is not defined before it is used to define TWO (constants are defined from left to right):
- CONST TWO = ONE + ONE, ONE = 1
- Constants declared in a SUB or FUNCTION procedure are local to the SUB or FUNCTION. A constant declared outside a procedure is defined throughout the module. You can use constants anywhere that you would use an expression.
- A common programming practice is to use a statement such as the following (any non-zero value represents "true"):
- TRUE = -1
- Using constants offers several advantages over using variables for constant values:
- You need only define constants once for an entire module.
- Constants cannot be inadvertently changed.
- In stand-alone programs, using constants produces faster and often smaller code than using variables.
- Constants make programs easier to modify.
Example
This example uses the CONST statement to declare symbolic constants for the ASCII values of nonprinting characters such as tab and line feed. Constants also make programs easier to modify.
Note: You must supply a text file when you run this example. Use a text file you have already created, create a file with a text editor, or specify the README.DOC text file.
'This program counts words, lines, and characters.
'A word is any sequence of nonblank characters.
DEFINT A-Z
CONST BLANK = 32, ENDFILE = 26, CR = 13, LF = 10
CONST TABC = 9, YES = -1, NO = 0
CLS 'Clear screen.
'Get the file name from the command line.
FileName$ = COMMAND$
IF FileName$ = "" THEN
INPUT "Enter input file name: ", FileName$
IF FileName$ = "" THEN END
END IF
OPEN FileName$ FOR INPUT AS #1
Words = 0
Lines = 0
Characters = 0
'Set a flag to indicate you're not looking at a
'word, then get the first character from the file.
InaWord = NO
DO UNTIL EOF(1)
C = ASC(INPUT$(1, #1))
Characters = Characters + 1
IF C = BLANK OR C = CR OR C = LF OR C = TABC THEN
'If the character is a blank, carriage return,
'line feed, or tab, you're not in a word.
IF C = CR THEN Lines = Lines + 1
InaWord = NO
ELSEIF InaWord = NO THEN
'The character is a printing character,
'so this is the start of a word.
'Count the word and set the flag.
InaWord = YES
Words = Words + 1
END IF
LOOP
PRINT Characters, Words, Lines
END