An if statement selects for execution one or none of the enclosed sequences of statements, depending on the (truth) value of one or more corresponding conditions.
if_statement ::= if condition then sequence_of_statements {elsif condition then sequence_of_statements} [else sequence_of_statements] end if; condition ::= boolean_expression
An expression specifying a condition must be of a boolean type.
For the execution of an if statement, the condition specified after if, and any conditions specified after elsif, are evaluated in succession (treating a final else as elsif TRUE then), until one evaluates to TRUE or all conditions are evaluated and yield FALSE. If one condition evaluates to TRUE, then the corresponding sequence of statements is executed; otherwise none of the sequences of statements is executed.
Examples:
if MONTH = DECEMBER and DAY = 31 then MONTH := JANUARY; DAY := 1; YEAR := YEAR + 1; end if; if LINE_TOO_SHORT then raise LAYOUT_ERROR; elsif LINE_FULL then NEW_LINE; PUT(ITEM); else PUT(ITEM); end if; if MY_CAR.OWNER.VEHICLE /= MY_CAR then -- see 3.8 REPORT ("Incorrect data"); end if;
References: boolean type, evaluation, expression, sequence of statements.
Rationale references: 2.2 Textual Structure, 3.9 If Statements
Style Guide references: 2.1.5 More on Alignment, 3.3.7 Marker Comments, 5.6.1 Nesting, 5.6.5 Exit Statements
Address any questions or comments to adainfo@sw-eng.falls-church.va.us.