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

OperationOperatorFeature
Sum+Calculates the sum of two or more variables.
DifferenceCalculates 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)ModCalculates the remainder of the division between the given data.

Scope

ScopeStatementFeature
ProcedureAt the beginning of the procedureVariables are available only within the procedure.
FormIn the general declarations section of the formVariables can be used in all procedures within the form where it was declared.
All FormsIn the general declarations section, use Public instead of DimVariables 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

OperatorSyntaxDescription
ANDAndRequires all conditions in the expression to be met for it to be true. If even one condition is not met, it becomes false.
OROrThe expression is true if one or more of the conditions are met. It is only false if *none* of the conditions are met.
NegationNotThis operator makes the expression true when the condition is *not* met, and false when it is met.