If statements determine if some condition is true, and then execute some sequence of statements depending on that determination. Here's a trivial example that determines if A is equal to B; if it is, A receives the value of B plus one. If A isn't equal to B, A receives the value of B minus one:
if A = B then A := B + 1; else A := B - 1; end if;
Here's the full BNF for the if statement:
if_statement ::= "if" condition "then" sequence_of_statements {"elsif" condition "then" sequence_of_statements} ["else" sequence_of_statements] "end if;"
Like other algorithmic languages, if `condition' is true the `then' part is executed. Otherwise, the elsif clauses (if any) are checked in first-to-last order, again looking for a true condition. Finally, if none of the conditions are true, the `else' clause is executed (if there's an "else" clause).
Notice that the keyword "then" is mandatory (it doesn't exist in C or C++).
What is the final value of A in the following sequence of statements?
A := 5; B := 6; if A > B then A := 7; else A := A - 2; end if;
![]() |
![]() |
![]() |
---|
David A. Wheeler (dwheeler@ida.org)