条件ステートメントを使用すると、ロジック・パーツで、所定の条件に応じて、1 つのアクションと複数のアクションのいずれかが選択されるようにすることができます。他のプロシージャー型プログラミング言語に精通しているユーザーは、おそらく、EGL で利用できる以下の条件ステートメントの使用方法について理解されているものと思われます。
if 文は EGL で最も単純な条件ステートメントです。if 文では、論理式の評価が行われ、その式が true である場合は、if 文内のコードが実行されます。その他の場合、if 文内のコードは実行されません。以下に、if 文の例を示します。
if (myVariable1 == myVariable2) //This code executes only if myVariable1 is equal to myVariable2 myVariable1 = 5; myVariable3 = 10; end
状況に応じて、if 文に else 文を追加することもできます。else 文内のコードは、if 文内の論理式が false の場合にのみ実行されます。以下に、else 文を含む if 文の例を示します。
if (myVariable1 == myVariable2)
//This code executes only if myVariable1 is equal to myVariable2
myVariable1 = 5;
myVariable3 = 10;
else
//This code executes only if myVariable1 is not equal to myVariable2
myFunction();
myVariable2 = 20;
end
if 文は、任意のレベルにネストすることができます。if 文をネストすると、以下の例のような、より複雑なロジックを実行することができます。
if (myVariable1 > myVariable2)
myMessage = "myVariable1 is greater than myVariable2";
else
if (myVariable1 < myVariable3)
myMessage = "myVariable1 is less than myVariable2";
else
myMessage = "myVariable1 and myVariable2 are equal";
end
end
1 つの if else 文で選択肢として使用できる EGL コードは 2 つだけです。1 つは論理式が true である場合に選択され、もう 1 つは論理式が false である場合に選択されます。コードにおける選択肢を増やすための 1 つの方法として、case 文を使用することが挙げられます。case 文は、式の評価を行い、選択可能な一連の値から一致するものを探します。以下に、case 文の例を示します。
case (myCharVariable)
when ("A")
myMessage2 = "myCharVariable equals A";
when ("B")
myMessage2 = "myCharVariable equals B";
otherwise
myMessage2 = "myCharVariable is not A or B";
end
条件ステートメントについて詳しくは、ヘルプ・トピック「if else 」および「case」を参照してください。
条件ステートメントの論理式を作成するには、2 つの変数を比較したり、条件ステートメントの条件を設定したりする論理演算子が必要になります。EGL で利用できる一般的な演算子の一部を以下に示します。
以下のステップでは、条件ステートメントを使用してユーザー入力に応答する Web ページを作成します。
IfTest
package pagehandlers;
PageHandler IfTest
{view="IfTest.jsp", onPageLoadFunction=onPageLoad}
//Variables
a int {value=10};
b int {value=20};
c int {value=30};
msg char(50);
Function onPageLoad()
end
Function testSimpleIf()
if (a > b)
msg = "a > b";
else
if (b > a)
msg = "b > a";
else
msg = "b = a";
end
end
end
Function compoundAnd()
if ((a > b) && (a > c))
msg = "a is highest value";
else
if ((b > a) && (b > c))
msg = "b is highest value";
else
if ((c > a) && (c > b))
msg = "c is highest value";
else
msg = "Two or three values are equal.";
end
end
end
end
Function compoundOr()
if ((a > b) || (a > c))
msg = "a > b or a > c";
else
if ((b > a) || (b > c))
msg = "b > a, or b > c";
else
if ((c > a) && (c > b))
msg = "c is highest (a and b are equal)";
else
msg = "All values are equal";
end
end
end
end
Function testNotEqual()
if (a != b)
msg = "a is not equal to b";
else
msg = "a equals b";
end
end
end
以下は、ここで追加したコードに関する技術面での注釈です。
「挿入のコントロール」ウィンドウは次のようになります。

このページには、PageHandler 内の 4 つの関数のそれぞれにバインドする 4 つのボタンが必要です。
ページは次のようになります。

これで、『演習 1.7: ループ』を開始する準備ができました。