FreeBasic Boolean Keyword
last modified June 16, 2025
The FreeBasic Boolean
keyword represents a data type that can
hold only two values: true or false. Boolean values are fundamental for
controlling program flow and making logical decisions.
Basic Definition
In FreeBasic, Boolean
is a built-in data type that occupies
1 byte of memory. It can only contain the values true
(non-zero)
or false
(zero).
Boolean variables are commonly used in conditional statements, loops, and logical operations. They make code more readable when dealing with yes/no or on/off scenarios.
Declaring Boolean Variables
This example shows how to declare and initialize Boolean variables.
Dim isReady As Boolean Dim hasPermission As Boolean = True Dim isComplete As Boolean = False Print "isReady: "; isReady Print "hasPermission: "; hasPermission Print "isComplete: "; isComplete
Here we declare three Boolean variables. The first is uninitialized and defaults to false. The others are explicitly set to true and false. FreeBasic prints 0 for false and -1 for true when displaying Boolean values.
Boolean in Conditional Statements
Boolean values are most commonly used to control program flow in conditionals.
Dim isLoggedIn As Boolean = True If isLoggedIn Then Print "Welcome back, user!" Else Print "Please log in to continue." End If
This example demonstrates using a Boolean variable in an If
statement. The condition evaluates to true, so the welcome message is
printed. Boolean variables make conditional logic more readable.
Boolean Operators
FreeBasic provides logical operators for combining Boolean values.
Dim hasAccount As Boolean = True Dim isVerified As Boolean = False Print "AND: "; hasAccount And isVerified Print "OR: "; hasAccount Or isVerified Print "NOT: "; Not hasAccount Print "XOR: "; hasAccount Xor isVerified
This code demonstrates Boolean operators: AND, OR, NOT, and XOR. The AND operator returns true only if both operands are true. OR returns true if either operand is true. NOT inverts the value.
Boolean Function Return Values
Functions can return Boolean values to indicate success or failure.
Function IsEven(n As Integer) As Boolean Return (n Mod 2) = 0 End Function Dim number As Integer = 42 If IsEven(number) Then Print number; " is even" Else Print number; " is odd" End If
The IsEven
function returns a Boolean indicating whether a
number is even. This makes the calling code very readable, as the function
name clearly expresses its purpose and return type.
Boolean Arrays
Arrays of Boolean values can be useful for tracking multiple true/false states.
Dim weekdays(1 To 7) As Boolean weekdays(2) = True ' Monday weekdays(3) = True ' Tuesday weekdays(5) = True ' Thursday For i As Integer = 1 To 7 Print "Day "; i; ": "; IIf(weekdays(i), "Working day", "Off day") Next
This example creates an array to track which days are workdays. We set
some days to true and others remain false by default. The IIf
function helps display meaningful strings based on the Boolean values.
Boolean with Comparison Operators
Comparison operations naturally result in Boolean values.
Dim a As Integer = 10 Dim b As Integer = 20 Dim result As Boolean = (a < b) Print "a < b: "; result Print "a = b: "; (a = b) Print "a > b: "; (a > b)
Here we store the result of a comparison in a Boolean variable. We also directly print comparison results. All comparisons evaluate to Boolean values in FreeBasic.
Boolean Type Conversion
FreeBasic automatically converts between Boolean and other numeric types.
Dim flag As Boolean = True Dim numericValue As Integer = flag Print "Boolean to Integer: "; numericValue Dim newFlag As Boolean = -1 Print "Integer to Boolean: "; newFlag
This demonstrates implicit type conversion. When converting to integer, true becomes -1 and false becomes 0. When converting from integer, any non-zero value becomes true, while zero becomes false.
Best Practices
- Initialization: Always initialize Boolean variables explicitly.
- Readability: Prefer Boolean variables over magic numbers in conditions.
- Functions: Use Boolean return types for yes/no questions.
- Operators: Use parentheses to clarify complex Boolean expressions.
This tutorial covered the FreeBasic Boolean
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.