Logic Basic tutorial for beginners
Conditional statements and flow control

If conditional statement

This is a statement that exists in all programming languages, and its function is to make the execution of a code snippet according to the result of an expression: if the expression is true, it executes the code snippet, if it is false does not run, or run other code snippet. The name of this statement is If, and it has two more auxiliary statements: Else and EndIf. The statement Else runs another code snippet if the result of the expression is false, and the statement EndIf must always be placed at the end of a clause If, for example:

Variable X Integer

X = 1
If X = 1
  Write "X is equal to 1!"
Else
  Write "X is not equal to 1!"
EndIf

The command Else is not mandatory, but the command EndIf must necessarily be placed to finalize their respective command if.

You can place a command If within another command If, or even cascading, for example:

Variable X Integer, Y Integer

X = 1
Y = 2

If X = 1
  Write "X is equal to 1"
  If Y = 2
    Write "Y is equal to 2"
  Else
    Write "Y is not equal to 2"
  EndIf
Else
  Write "X is not equal to 1"
EndIf

GoTo directional command

As seen previously, Logic Basic executes the program line by line, from the first to the last line. But you can direct the execution of the program to another program line with the command called GoTo, which causes program execution to go to a certain line, for this you must create a label (any word followed by a colon) in a particular line of the program, and direct the execution to the line of the label with the command GoTo, for example:

Variable X Integer

X = 2

If X = 1
  GoTo LABEL1
Else
  GoTo LABEL2
EndIf

LABEL1:
Write "Label 1"

LABEL2:
Write "Label 2"

While flow control statement

The Logic Basic has a flow control statement named While that makes a snippet of code runs repeatedly while the result of an expression is true. Each While statement should have its respective Loop statement, which should be placed at the end of the snippet of code to be executed. When the result of the expression is false, the program execution will be directed to the next line after the Loop statement, for example:

Variable X Integer

X = 0

While X < 10
  Write X
  X++
Loop

The above example will write the value of X while it's less than 10, it means that he will write all the values 0-9.

You can also put a statement While within another statement While, or even cascading, for example:

Variable X Integer, Y Integer

X = 0
While X <= 3
  Write "X = ", X
  X++
  Y = 0
  While Y <= 2
    Write "Y = ", Y
    Y++
  Loop
  Write ""
Loop

Back to index      Next topic