Keyword if
if is documented here as a full reference entry: grammatical role, semantics, canonical form, valid example, counter-example, diagnostics, interactions, and design notes.
if.Visual anchor: each page now has its own wiki-style profile image. It shows a small code excerpt where if appears in its most recognizable form.
Quick navigation: use the previous, summary, and next links to move through the full keyword series without manually returning to the index.
Summary
- Overview
- Definition
- Grammatical role
- Canonical syntax
- Detailed semantics
- Specific profile
- Effect on execution
- Valid variants
- Vitte example
- Guided reading of the example
- Comparison with C
- Recommended uses
- Invalid example and diagnostic
- Common errors
- Neighbor keywords
- Common misreadings
- Implementation notes
- Presence in the book
Overview
| Field | Value |
|---|---|
| Keyword | if |
| Family | Control flow |
| Suggested level | Beginner |
| Main neighbor | match |
| Short role | if is a control-flow keyword that changes the program's execution path. |
| Main effect | if immediately changes the execution path, whether by choosing a branch, repeating, interrupting, or terminating a flow. |
The keyword if structures execution. It determines when a block opens, when a branch is chosen, when a loop continues or stops, or when an exit becomes observable.
A useful encyclopedic reading should answer three questions: where can if appear, what does it change in the block contract, and how does the compiler signal misuse?
Definition
if is a control-flow keyword that changes the program's execution path.
The keyword if structures execution. It determines when a block opens, when a branch is chosen, when a loop continues or stops, or when an exit becomes observable.
Grammatical role
Introduces a conditional branch inside a procedure body, loop, or another control construction.
This grammatical role is essential: if a reader understands the structural place of if, they already understand much of the diagnostics that will appear when it is moved or truncated.
Canonical syntax
Canonical form: `if condition { ... }`.
The canonical form matters because it gives the compiler and the reader the same reference structure. A large share of diagnostics related to if come from an abbreviated, displaced, or incomplete form.
Detailed semantics
Semantically, if reorganizes the execution path. The right question is not only 'where is it placed?' but 'which path becomes possible, impossible, or preferred from this point onward?'
In an encyclopedic reading, if should not be reduced to a dictionary definition. Its effect on scope, block shape, value visibility, control progression, and the diagnostic family it activates when misused must also be considered.
Specific profile
Core keyword for local decisions. It does not merely test a condition: it makes the exact branch point of a flow visible in a context where the exit path must stay understandable.
Design notes
- The real issue with `if` is not the condition alone, but how the reader understands the exit paths of the block.
- A well-placed `if` prevents a domain rule or technical guard from remaining implicit inside a denser expression.
Reading questions
- Which contract is protected by this branch?
- What happens if the condition is false?
- Does the block exit remain obvious after reading the branch?
Targeted anti-patterns
- Stacking successive `if` blocks that hide case logic better expressed by `match` or `select`.
- Writing a valid but opaque condition that pushes the reading difficulty into the expression itself.
Effect on execution
if immediately changes the execution path, whether by choosing a branch, repeating, interrupting, or terminating a flow.
In other words, the presence of if is not merely syntactic: it helps the reader predict what will be executed, produced, exposed, or forbidden from this point in the program.
Valid variants
- `if condition { ... }`
- `if condition { ... } else { ... }`
These variants are not free synonyms. They indicate the legitimate forms from which one can reason about diagnostics, scope differences, or contract readability.
Vitte example
proc clamp_low(x: int) -> int {
if x < 0 { give 0 }
give x
}
This example shows if in a nominal context. It should be read globally: where the contract begins, which values are constrained, which output becomes observable, and why the presence of the keyword is justified.
Guided reading of the example
- First locate the full construction that contains
if, not the isolated word. - Then identify which contract becomes visible because of
if: type, branch, binding, module, exit, or advanced boundary. - Finish by checking the observable effect produced by the construction that contains
if. - For a control keyword, mentally reconstruct the possible paths and the ones that become impossible.
This guided reading is intentionally closer to a reference page than to a tutorial: it helps reconstruct the exact role of if in a complete block.
Comparison with C
int clamp_low(int x) {
if (x < 0) return 0;
return x;
}
This C comparison is structural: it aligns the role of the keyword with a familiar surface without claiming that the two languages carry exactly the same contracts.
The source of truth remains Vitte grammar and semantics. The comparison with C should be read as a cultural marker, not as a parallel specification.
Recommended uses
if deserves to appear when it simplifies the reading of the block's global contract, not when it merely adds one more surface form.
When to use it
- When
ifmakes the block contract more explicit at first reading. - When it reduces the number of implicit assumptions the reader must reconstruct mentally.
- When a branch choice, repetition, or flow exit must be made visible.
When to avoid it
- Avoid
ifwhen another, more precise keyword already carries the block's intent. - Avoid
ifwhen it adds only surface noise without clarifying the contract. - Avoid reading or teaching it as an isolated token with no relation to the full structure.
Common pitfalls
- Using
ifin a grammatical layer where it does not belong. - Confusing the role of the keyword with the role of the full surrounding block.
- Showing only the nominal form and never how the contract fails.
Invalid example and diagnostic
proc bad_branch(x: int) -> int {
if x { give 0 }
give x
}
The branch contract is broken because the condition surface is not valid.
The counter-example is not merely wrong: it is wrong in an instructive way. It shows which grammar or execution-contract assumption is no longer accepted when if is moved, truncated, or combined with the wrong context. Concretely, the condition does not have the valid form expected by a conditional branch.
A good encyclopedic counter-example does not show arbitrarily broken code: it isolates the precise reason why if can no longer support the expected contract. Its teaching value is diagnostic before it is syntactic.
Common compilation errors
| Typical message | Usual cause | Fix |
|---|---|---|
unexpected token near if | The keyword appears in an invalid form or grammatical layer. | Return to the canonical form and verify placement and delimiters. |
type mismatch | The keyword participates in a block whose value contract is incoherent. | Realign the surrounding types, branches, or produced values. |
invalid construct | The keyword is present but the surrounding construction is incomplete. | Restore the missing branch, declarative part, or operands. |
This table does not replace the compiler's exact diagnostics. It serves as a mental map: when if fails, the problem usually comes from an invalid grammatical form, an incoherent type contract, or an incomplete construction.
Neighbor keywords
| Keyword | Operational difference |
|---|---|
match | Direct neighboring keyword: it helps explain what if does, either by contrast or by complement. |
else | Completes the branch opened by if and avoids an implicit fallback path. |
when | Provides a specialized condition form in another selection setting. |
Comparison with neighboring keywords is essential on a wiki-style page: if is better understood when one knows precisely what it does not do.
Common misreadings
- Thinking that
ifdocuments only a local condition, when it actually restructures the entire exit path of the block.
Implementation and diagnostic notes
- Useful diagnostics for this family usually signal an incomplete branch shape, a mistyped condition, or an illegal placement in control flow.
- In a compiler, these keywords directly influence control-flow graph construction or intermediate representation building.