Feature request > IDE/Syntax
AndAlso
bigsofty:
The is a Freebasic command that is really for optimisation of IF statments. Instead of each expression being evaluated only the next one is evaluated if the previous one is true.
This can already be done using nested if statements but the AndAlso command, makes for a very clean and readable(in single line) IF/THEN statement and it makes for very efficient code when you dont need to evaluate extra conditions when one fails.
From the FB manual...
--- Code: ---If...Then Control flow statement for conditional branching
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 If
Description
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:
y <> 0 AndAlso x \ y = 1
(in this case, "x \ y = 1" will only be evaluated if "y <> 0" is True)
any numerical expression, in which case a value of zero (0) represents False, and a non-zero value represents True
Both multi-line and single-line Ifs can be nested. In the latter case, the optional End Ifs can be useful to control where nested Ifs begin and end.
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 low
Print "Too low"
ElseIf guess < num Then 'The user's guess is too high
Print "Too high"
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 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.
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.
Differences from QB
END IF was not supported in single-line IFs in QBASIC.
--- End code ---
spicypixel:
Could you not use SELECT and CASE to determine which IF expressions to evaluate?
bigsofty:
No I don't think so, that's not its purpose, SELECT/CASE is for a different situation, that is, executing a specific piece of code that matches a specific expression. AndAlso, is the same as AND, except in certain situation its more efficient. As I said, it can be recreated with nested ifs but its not as efficient or as neat.
--- Code: ---IF Check1(x1) ANDALSO Check1(x2) ANDALSO Check1(x3) ANDALSO x1 > 0 ANDALSO x1 <=10 THEN Blah=0
--- End code ---
One way to achieve this is...
--- Code: ---IF Check1(x1)
IF Check1(x2)
IF Check1(x3)
IF x1 > 0
IF x1 <= 10
blah=0
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
--- End code ---
The optimisation feature here is that the function "Check1()" may need only be called once if it fails early or that any failed expression can break out before the rest of the IFs expressions are evaluated. A lot less code may be executed when compared to a traditional IF/AND/THEN.
erico:
would be nice, my codes have a few of those nested ifs
Ocean:
what's wrong with:
--- Code: ---IF ( Check1(x1) AND Check1(x2) AND Check1(x3) AND x1 > 0 AND x1 <=10 ) THEN Blah=0
--- End code ---
?
cheers
Ocean
Navigation
[0] Message Index
[#] Next page
Go to full version