If statements are used to select whether a sequence of statements should be executed or not. In this way, they control the flow of the program. If there is no choice to be made, each statement in a sequence of instructions is simply executed one after another. A real-life example of this sort of sequencing would be the following:
However, sometimes you want to do something (or something is to be done) only if something else is happening or is true. A real-life example of this would be the following:
Many of us end up skipping breakfast because we don't have enough time. This kind of situation occurs frequently in programming.
The flowchart fragment illustrates Step 2. In this flowchart fragment, the testing of the condition is done in the diamond-shaped decision box. If the condition is found to be true, then the extra action, which is represented by the rectangular process box, is performed. (In the real-life example, this would be the eating of breakfast.) Otherwise, the extra action in the flowchart would simply be skipped.
In Pascal, we use the following if-statement construct to conditionally do something:
IF condition THEN
statement
The whole construct is considered to be a single Pascal statement. IF and THEN are reserved words. The condition is a boolean expression which evaluates to either TRUE or FALSE. THEN is used to introduce the statement inside the if statement. This statement is executed only if the condition is found to be TRUE. An example of this structure in Pascal would be:
IF num > 10 THEN
writeln('Your number is too large.')
The statement can represent any Pascal statement, e.g., a for statement or another if statement. The following example illustrates the use of a compound statement in this structure:
IF num < 0 THEN
BEGIN
num := 0;
writeln('The value was negative and has been reset to 0.')
END
Sometimes, you want to do one thing if something is happening (or is true) and something else otherwise. A real-life example of this would be the following:
The flowchart fragments illustrate two ways of showing Step 2.
To deal with two mutually exclusive choices in Pascal, we use the following if-statement construct:
IF condition
THEN
statementA
ELSE
statementB
Again, the whole construct is considered to be a single Pascal statement (exemplified by the indentation of all lines except the first) and the condition is a boolean expression which evaluates to either TRUE or FALSE. The statementA inside the if statement is executed only if the condition is found to be TRUE. ELSE is a reserved word which introduces statementB, which is executed only if the condition evaluates to FALSE.
Since the if construct is itself a statement which can be used inside another if statement, indentation levels can become numerous. Consequently, a more frequently used manner of indentation is shown below.
IF condition THEN statementA ELSE statementB
This kind of indentation reduces the number of levels, but obscures the fact that the whole conditional statement is a single statement in its own right. Thus, the placement of the ELSE keyword directly below the IF keyword incorrectly implies that it is on a par syntactically with IF. However, there are two good reasons for preferring this latter technique: (1) Since the if construct is itself a statement which can be used inside another if statement, indentation levels can become numerous. (2) It is easier to visualize the structure of the program, since an ELSE is associated with the previous outstanding IF.
An example of the double-option if-statement structure in Pascal would be:
IF num > 10 THEN
writeln('Your number is too large.')
ELSE
writeln('Your number is OK.')
N.B. These embedded statements could each be statements of any type, e.g., a for loop, a compound statement, or even another if statement (but that is another story). Therefore, the single-option if consists of a minimum of two statements while the double-option if consists of at least three statements.