Data Types, Variables, Operators, and Structures in BASIC
Data Behavior in a Program
Data can behave in two different ways in a program:
- Constants: Their value never changes during the program’s execution.
- Variables: Their value can vary as often as necessary.
Naming Conventions
When naming a variable, keep in mind the following rules:
- Any alphanumeric character can be used, but it must always begin with a letter.
- Spaces are not allowed; it is common to use the underscore character (_) for compound names.
- The name length may not exceed 32 characters.
- Dots or other special characters, as well as language keywords (text, caption, etc.), cannot be used.
Data Types and Declaration in BASIC
- String: String.
- Integer: Integer.
- Long Integer: Long.
- Single-precision Decimal: Single.
- Double-precision Decimal: Double.
- Date and Time: Date.
Variable declaration is made using the Dim
statement. If you declare a variable without defining the type, the program automatically assigns the type Variant.
The Print function displays literal texts and the values of variables in forms. The value of a constant cannot be modified during program execution.
Data Type Suffixes
- Character String: $
- Whole: %
- Long Integer: &
- Decimal: !
- Date and time: #
Arithmetic Operators
Operation | Operator | Feature |
---|---|---|
Sum | + | Calculates the sum of two or more variables. |
Difference | – | Calculates the difference between two data points. |
Multiplication | * | Multiplies the values of the variables. |
Division | / | Calculates the division of two data points or numerical variables. |
Exponentiation | ^ | Calculates the power, taking one variable as the base and the other as the exponent. |
Integer Division | \ | Calculates the quotient of the division of two data points or numeric variables. |
Remainder (Modulo) | Mod | Calculates the remainder of the division between the given data. |
Scope
Scope | Statement | Feature |
---|---|---|
Procedure | At the beginning of the procedure | Variables are available only within the procedure. |
Form | In the general declarations section of the form | Variables can be used in all procedures within the form where it was declared. |
All Forms | In the general declarations section, use Public instead of Dim | Variables declared as Public can be used in any form, but you must indicate their location when using the variable name. |
Control Structures
- Sequential Structures: These ensure the program executes in a specific order. Sentences are written sequentially, one after another, in the order they should be interpreted and run.
- Selective Structures: Used for making logical decisions, also known as alternative or decision structures. They evaluate a condition and, depending on the outcome, perform specific actions.
- Repetitive Structures: Also called loops, these structures repeat a sequence of statements a specified number of times.
Iteration: The execution of all actions or instructions that form a loop.
If Statement: Evaluates a condition and, if met, executes a series of specific instructions. Otherwise, the program will continue or perform alternative instructions.
Logical Operators
Operator | Syntax | Description |
---|---|---|
AND | And | Requires all conditions in the expression to be met for it to be true. If even one condition is not met, it becomes false. |
OR | Or | The expression is true if one or more of the conditions are met. It is only false if *none* of the conditions are met. |
Negation | Not | This operator makes the expression true when the condition is *not* met, and false when it is met. |