# Complex Conditions

In the complex case, the expression is any combination of constants, variables, functions, and class methods, joined together by relational and/or logical operators. Remember what you learned about operator precedence? Left to right evaluation applies to the relational and logical operators also. So, when building complex conditions, remember to always use parentheses to clarify the meaning of the expression.

Relational Operators

Operator | Operation |
---|---|

= | Equal To |

> | Greater Than |

< | Less Than |

>= | Greater Than or Equal To |

<= | Less Than or Equal To |

Logical Operators

Operator | Operation |
---|---|

&& | And - return true only if all conditions are true. |

|| | Or - return true if at least one condition is true. |

' | Not - return the reverse of the condition.
It can be used either outside a parenthesized condition or combined with a relational operator. |

Here are some examples:

VS Code - ObjectScript

/// examples for ObjectScript Tutorial Class ObjectScript.Examples { /// demos of many Ifs ClassMethod If() { set x = 5, y = 0, z = -5 if (x = 5) {write !, "x is equal to 5"} else {write !, "false"} if (x = 10) {write !, "x is equal to 10"} else {write !, "false"} if (x < y) {write !, "x is less than y"} else {write !, "false"} if (x > y) {write !, "x is greater than y"} else {write !, "false"} write ! if (##class(%SYSTEM.Util).NumberOfCPUs() > 2) {write !, "there are more than 2 CPUs"} else {write !, "false"} if (x > $zsqr(64)) {write !, "x is greater than square root of 64"} else {write !, "false"} write ! if (x && y) {write !, "both x and y are true (non-zero)"} else {write !, "false"} if (x && z) {write !, "both x and z are true (non-zero)"} else {write !, "false"} if (x && y && z) {write !, "x, y, and z are all true (non-zero)"} else {write !, "false"} if (x || y || z) {write !, "at least one of x, y, or z is true (non-zero)"} else {write !, "false"} write ! if ((x > y) || (y < z)) {write !, "either x is greater than y OR y is less than z"} else {write !, "false"} if (x > y || y < z) {write !, "without proper parentheses, this expression is false"} else {write !, "false"} if ((x > y) && (z < y)) {write !, "x is greater than y AND z is less than y"} else {write !, "false"} if (x > y && z < y) {write !, "without proper parentheses, this expression is also false"} else {write !, "false"} write ! if 'x {write !, "x is not true (zero)"} else {write !, "false"} if 'y {write !, "y is not true (zero)"} else {write !, "false"} if (x '< y) {write !, "x is not less than y"} else {write !, "false"} if '(x < y) {write !, "x is not less than y"} else {write !, "false"} } }

Copy code to clipboard

Testing using the Terminal

USER>do ##class(ObjectScript.Examples).If() x is equal to 5 false false x is greater than y there are more than 2 CPUs false false both x and z are true (non-zero) false at least one of x, y, or z is true (non-zero) either x is greater than y OR y is less than z false x is greater than y AND z is less than y false false y is not true (zero) x is not less than y x is not less than y USER>

Copy code to clipboard