Conditional statements allow your programs to make decisions and execute different code paths based on conditions. Conjure provides familiar syntax for conditional logic that should feel natural to anyone coming from C-like languages.
If Statements
The most basic conditional is the if statement, which executes a block of code only when a condition is true.
if x > 10 {
c.printf("x is greater than 10\n")
}The condition must be a boolean expression. Unlike some languages, Conjure does not allow integers or other types to be used directly as conditions. You must write explicit comparisons.
var count = 5
// This works
if count > 0 {
c.printf("We have items\n")
}
// This does not work - count is not a boolean
if count {
c.printf("This won't compile\n")
}Else Clauses
You can provide an alternative code path using else.
if temperature > 30 {
c.printf("It's hot outside\n")
} else {
c.printf("It's not too hot\n")
}Else If
Chain multiple conditions together using else if to check additional cases.
if score >= 90 {
c.printf("Grade: A\n")
} else if score >= 80 {
c.printf("Grade: B\n")
} else if score >= 70 {
c.printf("Grade: C\n")
} else if score >= 60 {
c.printf("Grade: D\n")
} else {
c.printf("Grade: F\n")
}Conditions are evaluated in order from top to bottom. Once a condition evaluates to true, its block is executed and the rest are skipped.
Constant Folding
One of Conjure’s powerful features is constant folding at compile time. When the compiler can determine the result of a conditional at compile time, it will eliminate the unused branches entirely.
const DEBUG = true
if DEBUG {
c.printf("Debug information: %d\n", value)
}If DEBUG is false, the entire if statement and its contents are removed from the compiled code. This means zero runtime overhead for debug logging or feature flags when they’re disabled.
const PLATFORM = "windows"
if PLATFORM == "windows" {
// Windows-specific code
initWindowsSubsystem()
} else if PLATFORM == "linux" {
// Linux-specific code
initLinuxSubsystem()
} else {
// Other platforms
initGenericSubsystem()
}The compiler will completely eliminate the branches for platforms you’re not targeting. This makes it easy to write cross-platform code without any runtime cost.
Enforced Constant Conditions Proposed
You an enforce that a condition must be a compile-time constant using the const keyword before the if. If the condition is not constant, the compiler will raise an error.
const DEBUG = true
var runtimeFlag = false
const if DEBUG {
c.printf("This will only compile if DEBUG is a constant\n")
}
const if runtimeFlag {
// This will cause a compile-time error
}Single-Line Bodies Proposed
When your conditional body contains only a single statement, you can write it on the same line with a semicolon or on the next line without braces. However, braces are always allowed and often preferred for clarity.
if x > 0; return x
if y < 0
y = -yInitialization in Conditionals Proposed
You can declare and initialize variables within the condition itself. The variable’s scope is limited to the if statement and any associated else clauses.
if var file = openFile("config.txt"); file != null {
defer file.close()
processFile(file)
} else {
c.printf("Could not open file\n")
}
// file is not accessible hereThis pattern is particularly useful for handling operations that might fail.
Nested Conditionals
Conditionals can be nested within each other as deeply as needed.
if userLoggedIn {
if hasPermission {
if resourceAvailable {
performAction()
}
}
}However, deeply nested conditionals can make code harder to read. Consider using early returns or the when statement for complex branching logic.
Combining Conditions
Use logical operators to combine multiple conditions in a single if statement.
if age >= 18 and hasLicense {
c.printf("Can drive\n")
}
if isWeekend or isHoliday {
c.printf("Day off!\n")
}
if not isValid {
c.printf("Invalid input\n")
}Remember that and and or use short-circuit evaluation. In a and b, if a is false, b is never evaluated. In a or b, if a is true, b is never evaluated.
Pattern Matching Alternative
For more complex conditional logic, especially when working with enums and unions, consider using the when statement instead. It provides exhaustive checking and cleaner syntax for matching against multiple values. See the Pattern Matching page for details.