fr de es it nl pl pt pt_BR mk sq ca ar fa vi ja ru zh zh_TW eo
Home > tutorial > testloop
 
Previous  Next  Edit  Rename  Undo  Refresh  Search  Administration   
Documentation
History
 
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 to perform some instructions according to if a condition is true or not.

If ... Then

This is the structure the most basic :

Syntax
If condition Then

Endif

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

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 affectation into a variable (like in a = 1).

Now, Here is an example to show that :

Syntax
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 :

Syntax
' 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 ELSE keyword is optional if there is only one instruction to execute :

Syntax
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 :

Syntax
Public Sub Main()

  Dim pays As string

  Print "De quel pays venez-vous ?"   Input pays   pays= lcase(pays) 'remove uppercases

  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 to repeat one or more instructions. Three loops type exists allowing to do the same thing in a different way.

For

The For boucle allows to repeat an instructions bloc n times.

Syntax
Public Sub Main()

  Dim i As Integer

  For i = 1 To 5     Print "Value of i : " & i   Next

End

When Gambas comes in into the loop for the first time, it affects the value 1 to i, then executes next instructions until it meets the NEXT keyword. Gambas return to the beginning of loop, then increments the i variable until it reachs 5. Instructions into the loop 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 loop to do. But we know how it must stop. Here is an example :

Syntax
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.

Syntax
Do
  Input result
Loop While result <> 4

While

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

Syntax
' Gambas module file

Public sub Main()

  Dim age As Integer

  While age < 10     Inc age     Print "Value of age : " & age   Wend

End

Result :

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.