Table of Contents
1. if
Conditionally executes a procedure based on a boolean value.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
(varies) |
Results from procedure (if executed) |
1.3. Description
id removes both operands from the stack, then executes proc if bool is true. If bool is false, proc is not executed.
The if operator itself pushes no results, but proc may push results onto the stack.
1.5. Examples
Simple conditional
3 4 lt { (3 is less than 4) } if
% Prints the string (condition is true)
Conditional definition
/DEBUG true def
DEBUG { (Debug mode enabled) print } if
No execution when false
false { (This won't print) } if
% Nothing happens
1.7. Common Pitfalls
Requires Boolean - Operand must be true or false, not 0/1 or other values.
|
1 { code } if % Error: typecheck
true { code } if % Correct
| Requires Procedure - Second operand must be executable array (procedure). |
true [code] if % Error: literal array
true { code } if % Correct: executable array
Use ifelse for Two-Way - For true/false cases, ifelse is more convenient than nested if statements.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Fewer than 2 operands on stack |
[ |
First operand not boolean, or second not procedure |