Test control and loop structures

Without these structures, our programs would work in a sequential way, and would be very poor.

Test structures

This allows us to perform some instructions if a condition is true.

If ... Then

This is the structure the most basic:

If condition Then

Endif

condition is the expression to test. The condition signs are:

  • > : purely upper

  • < : purely lower

  • >= : upper or equal

  • <= : lower or equal

  • = : equal

  • <> : unlike

Thus we can compare two values, two variables for example:

If Variable1 = Variable2 Then

Here, the "=" sign indicates an equality test, and not a value being assigned to a variable (like in a = 1).

Now, Here is an example to show that:

    Public Sub Main()
    
      Dim age As Integer
    
      Print "How old are you? "
      Input age
    
      If age >= 18 Then
        Print "You are major."
      Else
        Print "You are minor."
      Endif
    
    End

The ELSE keyword is optional. Example:

    ' Gambas module file
    
    Public Sub Main()
    
      Dim power As Integer = 15  'power in ch
    
      If puissance < 15 Then
        Print "Do you want to change your car? :p "
      Endif
    
    End

Another note, the ENDIF keyword is optional if there is only one instruction to execute:

    Public Sub Main()
    
      Dim power As Integer = 15  'power in ch
    
      If puissance < 15 Then Print "Do you want to change your car? :p "
    
    End

The Select ... Case structure

We're using this structure when there are many values to test. With this structure, our code is more clear to read:

    Public Sub Main()
    
      Dim pays As string
    
      Print "De quel pays venez-vous ?"
      Input pays
      pays= lcase(pays) 'remove uppercase
    
      Select Case pays
         Case "france"
            Print "Bonjour !"
         Case "england"
            Print "Hello!"
         Case "espana"
            Print "Ola!"
         Case "portugal"
            Print "Hola !"
         Case Else
            print"??"
      End Select
    
    End

Loops

Loops allows you to repeat one or more instructions. Three loops type exists allowing you to do the same thing in a different way.

For

The For loop allows to repeat an instructions block n times.

    Public Sub Main()
    
      Dim i As Integer
    
      For i = 1 To 5
        Print "Value of i: " & i
      Next
    
    End

When Gambas enters the loop for the first time, it applies the value 1 to i, then executes next instructions until it meets the NEXT keyword. Gambas returns to the beginning of loop, then increments the i variable until it reaches 5. The loop instructions will be read 5 times:

Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5

Do ... Loop

Sometimes we don't know the number of loops to do. But we know how it must stop. Here is an example:

    Public Sub Main()
    
      Dim result As integer
    
      Print "How many two and two?"
    
      Do
         Input result
      Loop Until result = 4
    
      PRINT "Congratulation! You have found :-) "
    
    End

We can also use the WHILE keyword instead of UNTIL to make a loop structure with a condition.

    Do
      Input result
    Loop While result <> 4

While

About WHILE, there is a loop structure specially for While:

    ' Gambas module file
    
    Public sub Main()
    
      Dim age As Integer
    
      While age < 10
        Inc age
        Print "Value of age : " & age
      Wend
    
    End

Value of age : 1
Value of age : 2
Value of age : 3
Value of age : 4
Value of age : 5
Value of age : 6
Value of age : 7
Value of age : 8
Value of age : 9
Value of age : 10

You can go to the next tutorial, we have finished with loops.