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 Turing, we use the following if-statement construct to conditionally do something:
if condition then
statement
end if
The whole construct is considered to be a single Turing statement. if, then and end if 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 Turing would be:
if num > 10 then put "Your number is too large." end if
The statement can represent any Turing 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 num := 0 put "The value was negative and has been reset to 0." end if
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 Turing, we use the following if-statement construct:
if condition then statementA ELSE statementB end if
Again, the whole construct is considered to be a single Turing statement 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.
An example of the double-option if-statement structure in Pascal would be:
if num > 10 then put "Your number is too large." ELSE put "Your number is OK." end if
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.