IF statement
The IF statement evaluates a Boolean expression to determine which of two possible courses of action to take. It
optionally includes an ELSE clause. Its syntax is shown here.
IF <boolean-expression>
THEN
<statement1>
[
ELSE
<statement2>
]
If <boolean-expression> evaluates as True, then <statement1> is executed; execution then
continues with the next statement after the IF statement. If <boolean-expression> evaluates False,
then <statement1> is skipped and <statement2> is executed.
Note that <statement1> and <statement2> can be compound statements (begin/end blocks),
or even another IF statement.
Confusion inevitably arises over IF statements and semicolon placement. It may help to remember that ELSEs are
considered part of the IF statement that immediately precedes it; they aren't statements in their own right. Since
the compiler interprets semicolons as statement separators, it doesn't make sense to have one inside a statement--and
that's just what you're doing if you put one directly before an ELSE.
program
IfDemo;
var
a,b: integer;
begin
Write('Enter values for A and B: ');
Readln(a,b);
if A
> B then
Writeln('A is greater than B') { no semicolon here }
else
Writeln('A is less than or equal to B');
end. { IfDemo }