We've just learned how to make conditional statements - but using only bool-casting in our conditions sure isn't enough. What we need is the ability to compare some values, and, possibly, perform some boolean logic. This chapter of the tutorial will introduce us to this topic.
:set &num i0 :read &num !if :gt $num i10 :writeln $num s' > 10' !fi !if :lt $num s"100" :writeln $num s' < "100"' !fi
The above example demonstrates the use of :gt
(greater than) and :lt
(lesser than) functions. Like arithmetic functions, they take pairs or arguments, starting from the rightmost one, and perform appropriate comparisons - returning TRUE if all comparisons are deemed true, or FALSE otherwise. Comparisons can be freely performed between different types.
Obviously, these aren't the only comparison functions available. For more information, including how different types are compared, check the documentation for the compare unit.
:set &num i0 :read &num !if :and (:ge $num i10 | :le $num i20) :writeln $num s' is in [10,20] range.' !fi !if :or (:not :mod $num i4 | :not :mod $num i5) :writeln $num s' is divisible by 4 or 5.' !fi
The above example demonstrates the use of :and
and :or
functions, which can be used to perform boolean logic and construct compound conditions. These functions operate on boolean values (other types will be cast silently) and perform the appropriate operation.
For more information about boolean logic functions, refer to the documentation for the boole unit.
!if :and (:ge $num i10 | :le $num i20) !fi !if (:ge $num i10 | :le $num i20) !fi !if :ge i20 $num i10 !fi
It has been said that the !if
construct automatically performs a logical AND on its arguments - which means, in the example above, we can skip the :and
call. If we also take into account that comparison functions check all argument pairs, we can replace the condition with a single :ge
call.
Next: ⇒ Arrays ⇒
Back: ⇐ Conditional statements ⇐
wikistrona zmodyfikowana 2014/0601/2317