Logic Basic tutorial for beginners
Variables declaration

In Logic Basic you can create variables, which are memories to store text, numbers and characters. There are three types of variables in Logic Basic: String, Integer and Decimal.

The type String should be used to declared variables or text string; The type Integer must be used to declare variables of type integer numeric to vary -2.147.483.648 to 2.147.483.647; The type Decimal should be used to declare variables of decimal number that can range from -1,79769313486232E308 to -4,94065645841247E-324 for negative values, and the 4,94065645841247E-324 to 1,79769313486232E308 for positive values. Simplifying, variables of type Decimal can be used to store both integer values as fractions, and are used primarily for working with currency values.

To declare variables you should use the keyword Variable or simply Var and then the variable name followed by its type. If not informed the variable type, the Logic Basic will assume to be of type String. You can declare multiple variables on the same line, for example:

Variable CustomerName String, Age Integer, Salary Decimal

or

Var CustomerName String, Age Integer, Salary Decimal

After the variables have been created, you can assign text and numbers for the variables using the = (equal) operator, for example:

CustomerName = "John Robinson"
Age = 50
Salary = 1234.50

Note that text must be enclosed in quotation marks, we will see why below...

When you assign text to a variable, the text must be enclosed in quotation marks. In most programming languages​​, a sequence of letters or characters are called Strings. From now on, treat texts as Strings to facilitate dialogue, and also you get to be familiar with this term. Therefore, when referring to Strings we are referring to texts, letters, characters, and numbers in text form.

A number, when quoted, is treated as a String, and when unquoted, is treated as a numeric value, it means that we can calculate with it.

When a number is assigned to a variable explicitly, if the number is fractional, decimal places should be separated by a decimal point, never use a comma, this is a rule.

The value of a variable can be assigned to another variable, for example:

X = 7
V = X

In the above example, the variable X received the number 7. Then the variable V received the value of the variable X, which is 7. Therefore, the content of the variable V is equal to 7.

For the Logic Basic, a number, or a variable containing a number, are seen as numbers. That is, 7 is a number, and X is a number. Only you can not see the number that is in X, but the Logic Basic yes, because it is in his memory. The same happens with strings, when a text is in a variable, the Logic Basic will work with its content and not its name. Let us illustrate with the following program:

Write CountryName

When you run the program, the result is the following word: CountryName. The Logic Basic wrote "CountryName" because that word is not a variable. Now let's modify the program to the following code:

Variable CountryName String
CountryName = "Canada"
Write CountryName

When you run the program, the result is as follows: Canada. The Logic Basic wrote "Canada" because the word "CountryName" was declared as a variable to the Logic Basic, and knowing this, he writes the variable content on the active window.

The name of a variable can not contain spaces in your body, so in the example above, the variable CountryName was not written Country Name. Variable names can not have the same name of the commands, keywords and reserved variables, and it is recommended that do not contain the name of commands in your body.

Global and Local Variables

The Logic Basic has the concept of Global and Local variables. Global variables can be read at any point in the program, they retain their value within the main code, extensions, subroutines and functions. Local variables must be declared only within functions (or subroutines), and they retain their value only within the function where it is declared and after the execution of the function, they are destroyed by Basic Logic. This is important because it saves memory and avoids confusion in your program. Local variables can have the same name in different functions without causing conflicts.


Rule for local variables declaration

To declare a local variable must meet one rule: they can only be declared in the first lines of a function, eg:

Function Sum() Integer
Variable X Integer, Y Integer
Variable Ret Integer

X = 10; Y = 20
Ret = X + Y

Return Ret
EndFunction

In the above example, the variables Ret, X and Y are local variables. If a variable is declared in the middle of the code of a function, it will be considered as Global, for example:

Function Sum() Integer
Variable X Integer, Y Integer

X = 10; Y = 20

Variable Ret Integer

Ret = X + Y

Return Ret
EndFunction

In the above example the variables X and Y are local, and the variable Ret is global.


Back to index      Next topic