Short circiut (||, &&)
This commit is contained in:
parent
3a2afdea69
commit
05818899fe
2 changed files with 139 additions and 14 deletions
36
examples/short_circuit_test.fun
Executable file
36
examples/short_circuit_test.fun
Executable file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
// Short-circuit demo for || and &&
|
||||
|
||||
print("=== short-circuit demo ===")
|
||||
|
||||
fun mark(name, v)
|
||||
// Show when this branch is evaluated
|
||||
print(name)
|
||||
return v
|
||||
|
||||
// OR short-circuits: RHS not evaluated when LHS is true
|
||||
if (true || mark("OR-RHS", 0))
|
||||
print(1) // expect: prints 1; no "OR-RHS"
|
||||
else
|
||||
print(0)
|
||||
|
||||
// AND short-circuits: RHS not evaluated when LHS is false
|
||||
if (false && mark("AND-RHS", 1))
|
||||
print(1)
|
||||
else
|
||||
print(0) // expect: prints 0; no "AND-RHS"
|
||||
|
||||
// OR evaluates RHS when needed (LHS false)
|
||||
if (false || mark("OR-NEEDED", 1))
|
||||
print(1) // expect: prints "OR-NEEDED" then 1
|
||||
else
|
||||
print(0)
|
||||
|
||||
// AND evaluates RHS when needed (LHS true)
|
||||
if (true && mark("AND-NEEDED", 1))
|
||||
print(1) // expect: prints "AND-NEEDED" then 1
|
||||
else
|
||||
print(0)
|
||||
|
||||
print("=== end ===")
|
||||
Loading…
Add table
Add a link
Reference in a new issue