1

How would I go about having multiple conditionals in an if statement?

Eg. The user is asked a set of questions by the program:

1.) Enter an altitude between 0 and 1000

(user types in data)

2.) Enter Velocity between 0 and 500

(user types in data)

3.) Enter Temperature between 0 and 200

(user types in data)

the program then prints back

  1. altitude = user value
  2. velocity = user value
  3. temperature = user value //ignore those list numbers

I have set in my (.ads) file that each of these ranges has a criticle value.

I want to create an if statement that has multiple conditionals. in pseudo : If velocity = critical velocity & temperature = critical temperature & altitude = critical altitude then print ("some message") else do nothing

DaveSwans
  • 57
  • 1
  • 2
  • 10

2 Answers2

5

The syntax of an if-statement is

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

and the syntax of “condition” is

condition ::= boolean_expression

(that is, an expression which happens to be boolean); the syntax of “expression” is

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

so your code would look like

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

You can leave out the else clause, and you can say and then instead of plain and if for some reason you shouldn’t check the rest of the condition if the first part is already False. This is called short-circuit evaluation, and it’s not the default in Ada (it is in C).

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

evaluates Y / X even if X is 0.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • Thank you Simon! A very iterative breakdown, very helpful :) – DaveSwans Apr 30 '16 at 14:40
  • here is a link to another question that I couldnt ask in the comments because it was too long for a comment. If you could please shed some light it would be much appreciated. http://stackoverflow.com/questions/36957726/shortened-method-to-reduce-statements-in-an-ada-procedure – DaveSwans Apr 30 '16 at 17:04
4

In Ada you will use the and, or and not boolean operator:

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

When evaluation order matters, you will use the and then or the or else syntax (otherwise the compiler can change order for optimization). Expressions will be evaluated in the 'and then'/'or else' order.

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

With an or else, you may write as follows:

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

Beware that you cannot mix and and or together (because this leads to many confusion for developers). You have to use parenthesis arround them if you do it.

if (Velocity = Critical_Velocity and Temperature = Critical_Temperature)
  or else Altitude = Critical_Altitude
then
    Ada.Text_IO.Put_Line ("Crash");
else
  ...
end if;
ciceron
  • 586
  • 5
  • 10