Skip to main content

This documentation is for an older version of this product. See the latest version of this content.Opens in a new tab

複雑な条件

複雑なケースでは、定数、変数、関数、およびクラス・メソッドを関係演算子や論理演算子で結合して組み合わせた式になります。演算子の優先順位で学習したことを思い出してください。関係演算子と論理演算子も左から右に評価されます。したがって、複合条件を指定する場合、括弧を使用して式をわかりやすく表現します。

関係演算子
演算子 演算
= 等しい
> より大きい
< より小さい
>= 以上
<= 以下
論理演算子
演算子 演算
&& And - すべての条件が True の場合にのみ True を返します。
|| Or - 条件の 1 つ以上が True の場合に True を返します。
' Not - 条件の逆を返します。

括弧で囲んだ条件の外部で使用することも、関係演算子と組み合わせることもできます。

以下は、その例です。

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"}
}
}
ターミナルを使用したテスト


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>
FeedbackOpens in a new tab