Repository navigation
Operator Andalso Short Circuit Conjunction
Home › Operators › Operator Andalso (Short Circuit Conjunction)
Returns the short circuit-and (conjunction) of two numeric values
Declare Operator AndAlso ( ByRef lhs As T1, ByRef rhs As T2 ) As RetlhsT1rhsT2RetT1T2
This operator evaluates the left hand side expression. If the result is zero, then zero is immediately returned. If the result is nonzero then the right hand side is evaluated, and the logical result from that is returned.
(for conversion of a boolean to an integer, false or true boolean value becomes 0 or -1 integer value)
The truth table below demonstrates all combinations of a short circuit-and operation, the '-' denotes that the operand is not evaluated.
| Lhs Value | Rhs Value | Result |
|---|---|---|
| 0 | (not evaluated) | 0 |
| nonzero | 0 | 0 |
| nonzero | nonzero | -1 |
AndAlso equivalent to:
Iif( Lhs <> 0, Rhs <> 0, 0 )
Short-circuiting is performed - only expressions needed to calculate the result are evaluated. The left hand side lhs is evaluated first, and only if it evaluates to non-zero (true) is the right hand side rhs also evaluated. If the left hand side evaluation lhs returns zero (false), it is known that at that point that the overall condition is false, so the right hand side rhs is not evaluated (skipped).
The return type is almost always an Integer, of the value 0 or -1, denoting false and true respectively. Except if the left and right-hand side types are both Boolean, then the return type is also Boolean.
This operator cannot be overloaded for user-defined types.
Returns the short circuit-and (conjunction) of the two operands.
result = lhs AndAlso rhs
- This operator was not available in QB.
'' Using the ANDALSO operator to guard against array access
'' when the index is out of range
Dim As Integer isprime(1 To 10) = { _
_ ' 1 2 3 4 5 6 7 8 9 10 (2, 3, 5 and 7 are the only prime numbers)
0, 1, 1, 0, 1, 0, 1, 0, 0, 0 _
}
Dim As Integer n
Input "Enter a number between 1 and 10: ", n
'' isprime() array will only be accessed if n is in range
If (n >= 1 And n <= 10) AndAlso isprime(n) Then
Print "n is prime"
Else
Print "n is not prime, or out of range"
End If
SleepCategory: Operators
Kind: operator
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing