FuncIf

FuncIf

FuncIf[ cond, a, b ] returns a if a condition is true and b if it is false.

Usages

FuncIf[ cond, a, b ]

FuncIf[ cond, a, b ] is the expression form of If ... Else ... End: it returns a if cond is True, and b if cond is False. Only the chosen expression is executed, so the other one may even be invalid.

Example in out
The condition is true. FuncIf[ 5 >> 3, a, b ] a
The condition is false. FuncIf[ 3 >> 5, a, b ] b
Choose a text. n := 4
r := FuncIf[Mod[n, 2] == 0, Text[`even`], Text[`odd`]]
r = Text[`even`]
L[[2]] does not exist, but it is not executed. L := {7}
r := FuncIf[Length[L] == 1, 0, L[[2]]]
r = 0
Details
  • Undecided condition: if cond cannot be decided, FuncIf is left unevaluated: FuncIf[ x >> 3, a, b ] stays FuncIf[x >> 3, a, b]. Use Strict to choose b in that case: FuncIf[ Strict[x >> 3], a, b ] gives b.
Warning

Use comparisons that are evaluated directly, such as ==, >>, <<, >==, <== and <<>>. With >, <, >=, <= or <>, the condition is not evaluated and FuncIf is left unevaluated: FuncIf[ 3 > 5, a, b ] stays FuncIf[3 > 5, a, b].

See also: If, Strict, Greater, EqualsEval