If statements#
Code Example
Runnable Example in Jac and JacLib
# If Statements
# Basic if statement
glob x = 10;
with entry {
if x > 5 {
print("x is greater than 5");
}
}
# If-else statement
glob age = 18;
with entry {
if age >= 18 {
print("adult");
} else {
print("minor");
}
}
# If-elif-else chain
glob score = 85;
with entry {
if score >= 90 {
print("A");
} elif score >= 80 {
print("B");
} elif score >= 70 {
print("C");
} else {
print("F");
}
}
# Multiple elif chains
glob value = 15;
with entry {
if value < 5 {
print("very low");
} elif value < 10 {
print("low");
} elif value < 15 {
print("medium");
} elif value < 20 {
print("high");
} else {
print("very high");
}
}
# Nested if statements
glob a = 15,
b = 20;
with entry {
if a > 10 {
print("a > 10");
if b > 15 {
print("b > 15");
if a + b > 30 {
print("a + b > 30");
}
}
}
# Complex boolean expressions
if a > 5 and b > 10 {
print("both conditions true");
}
if a > 100 or b > 15 {
print("at least one true");
}
if not (a > 50) {
print("negation true");
}
if (a > 5 and b > 10) or (a < 20) {
print("complex expression true");
}
}
# Chained comparisons
glob temp = 25;
with entry {
if 20 <= temp <= 30 {
print("comfortable temperature");
}
}
# Membership tests
glob fruits = ["apple", "banana"];
with entry {
if "apple" in fruits {
print("apple found");
}
if "grape" not in fruits {
print("grape not found");
}
}
# Identity tests
glob val = None;
with entry {
if val is None {
print("val is None");
}
if val is not None {
print("val is not None");
} else {
print("val is None");
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
Jac Grammar Snippet
Description
If Statements in Jac
If statements provide conditional control flow through if, elif, and else keywords. They allow code to execute different paths based on boolean conditions.
Basic If Statement
Lines 4-8 demonstrate the simplest conditional. When the condition x > 5 evaluates to true, the block executes.
If-Else Statement
Lines 11-16 show binary choice logic. Exactly one block executes: the if block when true, otherwise the else block.
If-Elif-Else Chain
Lines 19-28 demonstrate multiple exclusive conditions. Evaluation is top-down. The first true condition executes, then the entire chain terminates.
Multiple Elif Branches
Lines 31-42 show extended conditional chains with many branches. Each elif provides an additional condition to check if previous conditions were false.
Nested If Statements
Lines 45-55 demonstrate if statements within if statements. Inner conditions only evaluate if outer conditions are true.
Complex Boolean Expressions
Lines 58-72 show combining conditions with logical operators:
AND operator (line 58-60):
Both conditions must be true.
OR operator (line 62-64):
At least one condition must be true.
NOT operator (line 66-68):
Negates the condition.
Combined operators (line 70-72):
Parentheses control precedence.
Chained Comparisons
Lines 75-78 demonstrate Python-style chained comparisons. Equivalent to 20 <= temp and temp <= 30, but evaluates temp only once.
Membership Tests
Lines 81-88 show in and not in operators. Works with any iterable: lists, tuples, sets, dictionaries (checks keys), strings.
Identity Tests
Lines 91-100 demonstrate is and is not for identity checking. Use is for None checks, not ==.
Comparison Operators
| Operator | Meaning | Example Line |
|---|---|---|
== |
Equal | Throughout |
!= |
Not equal | Implicit |
< |
Less than | 32 |
<= |
Less than or equal | 76 |
> |
Greater than | 6 |
>= |
Greater than or equal | 12, 20 |
is |
Identity | 92 |
is not |
Not identity | 96 |
in |
Membership | 82 |
not in |
Non-membership | 86 |
Logical Operators
| Operator | Meaning | Precedence |
|---|---|---|
not |
Negation | Highest |
and |
Both must be true | Medium |
or |
At least one must be true | Lowest |
Control Flow Diagram
flowchart TD
Start([If Statement]) --> Cond1{If Condition<br/>True?}
Cond1 -->|Yes| IfBlock[Execute If Block]
Cond1 -->|No| Elif{Elif<br/>Present?}
IfBlock --> Done([Continue])
Elif -->|Yes| ElifCond{Elif Condition<br/>True?}
Elif -->|No| Else{Else<br/>Present?}
ElifCond -->|Yes| ElifBlock[Execute Elif Block]
ElifCond -->|No| NextElif{More<br/>Elif?}
ElifBlock --> Done
NextElif -->|Yes| ElifCond
NextElif -->|No| Else
Else -->|Yes| ElseBlock[Execute Else Block]
Else -->|No| Done
ElseBlock --> Done
Truthy and Falsy Values
Jac follows Python's truthiness rules:
Falsy values:
FalseNone0(numeric zero)""(empty string)[](empty list){}(empty dict)
Truthy values:
- Everything else
Example:
Common Patterns
Guard pattern (early return):
Range checking:
None checking:
Membership filtering:
Multi-condition validation:
Short-Circuit Evaluation
and and or use short-circuit evaluation:
If x != 0 is false, y / x doesn't execute (preventing division by zero).
If Expression (Ternary)
Jac supports if expressions for value selection:
If expressions must have an else clause and return a value.
Block Syntax Rules
- Braces required:
{ }delimit blocks, not indentation - Semicolons required: Each statement ends with
; - Single-line allowed:
if x > 5 { print("yes"); }is valid - Multi-line preferred: For readability
If Statement vs If Expression
| Feature | If Statement | If Expression |
|---|---|---|
| Syntax | if cond { ... } else { ... } |
val1 if cond else val2 |
| Returns value | No | Yes |
| Multiple statements | Yes | No (expressions only) |
| Else required | No | Yes |
| Use case | Control flow | Value selection |
Key Differences from Python
- Braces required: Jac uses
{ }, not indentation - Semicolons required: Each statement ends with
; - Same truthiness: Empty collections are falsy
- Same operators:
and,or,not,in,is - Same chaining:
a <= b <= cworks identically