If...Then
Control flow statement for conditional branching
If expression Then [statement(s)] [Else [statement(s)]] [End If]
or
If expression Then : [statement(s)] [Else [statement(s)]] : End If
or
If expression Then
If...Then is a way to make decisions. It is a mechanism to execute code only if a condition is true, and can provide alternative code to execute based on more conditions.
expression can be one of several forms:
In the -lang fb and -lang fblite dialects, colons (:) can be used instead of newlines to construct multi-line If blocks on a single line.
Syntax
If expression Then [statement(s)] [Else [statement(s)]] [End If]
or
If expression Then : [statement(s)] [Else [statement(s)]] : End If
or
If expression Then
[statement(s)]
[ ElseIf expression Then ][statement(s)]
[ Else ][statement(s)]
End IfDescription
If...Then is a way to make decisions. It is a mechanism to execute code only if a condition is true, and can provide alternative code to execute based on more conditions.
expression can be one of several forms:
- a conditional expression, for example:
x = 5
- multiple conditions separated by logical bit-wise operators, for example:
x >= 5 And x <= 10
- multiple conditions separated by logical short-circuit operators, for example:
- any numerical expression, in which case a value of zero (0) represents False, and a non-zero value represents True
In the -lang fb and -lang fblite dialects, colons (:) can be used instead of newlines to construct multi-line If blocks on a single line.
Example
'' Here is a simple "guess the number" game using if...then for a decision.
Dim As Integer num, guess
Randomize
num = Int(Rnd * 10) + 1 'Create a random number between 1 and 10...
Print "guess the number between 1 and 10"
Do 'Start a loop
Input "Guess"; guess 'Input a number from the user
If guess > 10 OrElse guess < 1 Then 'The user's guess is out of range
Print "The number can't be greater then 10 or less than 1!"
ElseIf guess > num Then 'The user's guess is too high
Print "Too high"
ElseIf guess < num Then 'The user's guess is too low
Print "Too low"
ElseIf guess = num Then 'The user guessed the right number!
Print "Correct!"
Exit Do 'Exit the loop
End If
Loop 'Go back to the start of the loop
Dim As Integer num, guess
Randomize
num = Int(Rnd * 10) + 1 'Create a random number between 1 and 10...
Print "guess the number between 1 and 10"
Do 'Start a loop
Input "Guess"; guess 'Input a number from the user
If guess > 10 OrElse guess < 1 Then 'The user's guess is out of range
Print "The number can't be greater then 10 or less than 1!"
ElseIf guess > num Then 'The user's guess is too high
Print "Too high"
ElseIf guess < num Then 'The user's guess is too low
Print "Too low"
ElseIf guess = num Then 'The user guessed the right number!
Print "Correct!"
Exit Do 'Exit the loop
End If
Loop 'Go back to the start of the loop
Dialect Differences
- In the -lang qb and -lang fblite dialects, variables declared inside an If..Then block have a procedure-wide scope as in QB
- In the -lang fb and -lang deprecated dialects, variables declared inside an If..Then block are visible only inside the block, and cannot be accessed outside it.
- In the -lang qb dialect, if there is a new line or a single-line comment (') directly after THEN, then the IF will be multi-line. A colon, a Rem or any other statement will result in a single-line IF.
- In the -lang fb and -lang fblite dialects, if there is a new line, a single-line comment ('), a colon (:), or a Rem statement directly after THEN, then the IF will be multi-line. Any other statement will result in a single-line IF.
Differences from QB
- END IF was not supported in single-line IFs in QBASIC.
See also