2016-04-28 14 views
1

if文で複数の条件文を使用する方法を教えてください。Adaのif文で複数の条件文を使用する方法

例:データにおける0〜

1000(ユーザタイプ)

2)が0と500

間の速度を入力して高度を入力し

1):ユーザがプログラムによる一連の質問を尋ねられます

(データ内のユーザタイプ)

3)データで0と200

(ユーザタイプとの間の温度を入力してください)

プログラムは次に

  1. 高度=ユーザー値
  2. 速度=ユーザー値
  3. 温度=ユーザー値//私は(に設定されているもののリスト番号

を無視裏面印刷.ads)ファイルには、これらの範囲のそれぞれに基準値があります。

複数の条件文を持つif文を作成したいとします。 擬似に:速度=臨界速度&温度=臨界温度&高度=臨界次いで高度 プリント(「一部のメッセージ」)が は何もしない場合

答えて

5

syntax of an if-statement

if_statement ::= 
    if condition then 
     sequence_of_statements 
    {elsif condition then 
     sequence_of_statements} 
    [else 
     sequence_of_statements] 
    end if; 

あり、syntax of “condition”であります

condition ::= boolean_expression 

(つまり、真にブール値の式です)。 syntax of “expression”ので、あなたのコードが

if velocity = critical_velocity 
    and temperature = critical_temperature 
    and altitude = critical_altitude 
then 
    print ("some message”); 
else 
    null; 
end if; 

のようになります。あなたはelse句を残すことができ、そして何らかの理由であなたがチェックしてはならない場合は、and then代わりの平野andを言うことができ

expression ::= 
    relation {and relation} | relation {and then relation} 
    | relation {or relation} | relation {or else relation} 
    | relation {xor relation} 

です最初の部分が既にFalseであれば、残りの状態。これは短絡評価と呼ばれ、ではなく、がAdaのデフォルト(C言語)です。

if X /= 0 and Y/X > 2 then 

は、Xが0であってもY/Xと評価されます。

if Velocity = Critical_Velocity 
    and Temperature = Critical_Temperature 
    and Altitude = Critical_Altitude 
then 
    Ada.Text_IO.Put_Line ("Crash"); 
else 
    ... 
end if; 

が評価順序の問題は、あなたが、その後またはまたは他を使用します。

+0

ありがとうサイモン!非常に反復的な内訳、非常に役に立ちます:) – DaveSwans

+0

ここにコメントのためには長すぎたので私がコメントで尋ねることができなかった別の質問へのリンクです。あなたはいくつかの光を発することができれば、それは非常に高く評価されるだろう。 http://stackoverflow.com/questions/36957726/shortened-method-to-reduce-statements-in-an-ada-procedure – DaveSwans

4

エイダでは、またはないブール演算子を使用します(そうしないと、コンパイラは最適化のために順序を変更できます)。 式は 'と'/'またはそれ以外の順序で評価されます。次のようにか、他

if Velocity = Critical_Velocity 
    and then Temperature = Critical_Temperature 
    and then Altitude = Critical_Altitude 
then 
    Ada.Text_IO.Put_Line ("Crash"); 
else 
    ... 
end if; 

、あなたが書くことがあります。

if Velocity = Critical_Velocity 
    or else Temperature = Critical_Temperature 
    or else Altitude = Critical_Altitude 
then 
    Ada.Text_IO.Put_Line ("Crash"); 
else 
    ... 
end if; 

を(これは開発者のために多くの混乱につながるので)あなたが一緒にまたはを混在させることはできませんので注意してください。 あなたがそれを行うならば、それらを丸めて使用する必要があります。

if (Velocity = Critical_Velocity and Temperature = Critical_Temperature) 
    or else Altitude = Critical_Altitude 
then 
    Ada.Text_IO.Put_Line ("Crash"); 
else 
    ... 
end if; 
+0

xorもあります。 – darkestkhan