Dav*_*ans 1 conditional if-statement ada
我如何在if语句中使用多个条件?
例如.程序会向用户询问一组问题:
1.)输入0到1000之间的高度
(用户输入数据)
2.)输入0到500之间的速度
(用户输入数据)
3.)输入0到200之间的温度
(用户输入数据)
该程序然后打印回来
我在我的(.ads)文件中设置了每个范围都有一个临界值.
我想创建一个具有多个条件的if语句.in pseudo:如果速度=临界速度和温度=临界温度和高度=临界高度然后打印("某些消息")否则什么都不做
if_statement ::=
if condition then
sequence_of_statements
{elsif condition then
sequence_of_statements}
[else
sequence_of_statements]
end if;
Run Code Online (Sandbox Code Playgroud)
condition ::= boolean_expression
Run Code Online (Sandbox Code Playgroud)
expression ::=
relation {and relation} | relation {and then relation}
| relation {or relation} | relation {or else relation}
| relation {xor relation}
Run Code Online (Sandbox Code Playgroud)
所以你的代码看起来像
if velocity = critical_velocity
and temperature = critical_temperature
and altitude = critical_altitude
then
print ("some message”);
else
null;
end if;
Run Code Online (Sandbox Code Playgroud)
您可以省略该else条款,如果出于某种原因,如果第一部分已经存在,则不应检查其余条件,and then而是可以说明文.这称为短路评估,它不是 Ada中的默认值(它在C中).andFalse
if X /= 0 and Y / X > 2 then
Run Code Online (Sandbox Code Playgroud)
Y / X即使X为0也要进行评估.