> tutorial > testloop| Documentation |
|
This, allows to perform some instructions according to if a condition is true or not.
This is the structure the most basic :
If condition ThenEndif
condition is the expression to test. The condition signs are :
![]() | 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 :
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 filePublic 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 :
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
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 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 allows to repeat one or more instructions. Three loops type exists allowing to do the same thing in a different way.
The For boucle allows to repeat an instructions bloc n times.
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 :
Sometimes we don't know the number of loop 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
About WHILE, there is a loop structure specially for While :
' Gambas module filePublic sub Main()
Dim age As Integer
While age < 10 Inc age Print "Value of age : " & age Wend
End
Result :
You can go to the next tutorial, we have finished with loops.