When discussing the general features of problem solving we considered that there were three sorts of activity that we might engage in in order to reach a solution: sequence, choice, and repetition. Modula 3 allows us to make choices and to perform actions repeatedly. In this section we look at one of the ways of making choices \(em the if-statement.
All of the examples of choice that we met involved asking a question, `Is there enough petrol in the tank?', `Is there enough money in the account?', `Is the password correct?' The same is the case in Modula 3: there will always be a question to be asked. This is often spoken of as `a condition to be evaluated.' In English the answer to a question can be many things, `Yes', `No', `Maybe', `I'm not sure', `Not, today', `If you like', and so on. In Modula 3 things are a lot more straightforward as the answer to a question is always either `Yes' or `No'. In Modula 3 these are referred to as TRUE and FALSE. With every condition to be tested there will be an associated action if that condition proves TRUE. This is expressed in Modula 3 by saying:
IF condition THEN action END
Consider programming a cash dispenser. Before supplying the money it is necessary to check that there is at least this amount in the user's account. We could ask this in several ways, for instance, `Is there enough money in the account?' or `Does the user want to withdraw too much money?'. Both can be answered `Yes' (TRUE) or `No' (FALSE), but they are asking opposite things. Here is an example of a simple if-statement in Modula 3 that asks a similar sort of question:
(* Assume that the real variable AccountBalance
* holds the user's current bank balance.
*)
Put.Text("How much do you wish to withdraw?\n");
Get.Real(Withdrawal);
IF Withdrawal > AccountBalance THEN
Put.Text("Your account does not have enough money in it.\n");
END;
When the user supplies an amount to withdraw, this is remembered in the variable Withdrawal. The condition
Withdrawal > AccountBalance
asks `Is the value in Withdrawal greater than the value in AccountBalance?' If the answer is `Yes' - TRUE - then the withdrawal must be denied, and a message printed.
Can you write a similar example piece of code that asks the user for their weight in Kilograms or Pounds and prints a message about losing weight if this value is too large?
A second example illustrates that we can specify different actions to be taken depending on whether the condition is TRUE or FALSE:
(* Assume that the real variable AccountBalance
* holds the user's current bank balance.
*)
Put.Text("How much do you wish to withdraw?\n");
Get.Real(Withdrawal);
IF Withdrawal <= AccountBalance THEN
DispenseTheMoney;
AccountBalance := AccountBalance-Withdrawal;
Put.Text("Have a nice day!\n");
ELSE
Put.Text("Your account does not have enough money in it.\n");
END
This time I have turned the question around so that we are asking, `Is there enough money in the account?' If there is enough then the program must invoke an action to dispense the money in some way (not specified), it then subtracts the withdrawal amount from the current balance and prints a friendly message. If there isn't enough money then the part of the if-statement marked ELSE is the part that is acted on: a message is printed. This form of if-statement is sometimes called an if-then-else-statement.
Can you rewrite the if-then-else example so that it uses the original test
Withdrawal > AccountBalance
and still works properly?
In the above examples we used examples of two relational operators: '>' (`greater than') and '<=' (`less than or equal to'). There are four more that you will meet regularly when writing conditions to test between two arithmetic expressions. Here is the complete set:
> greater than < less than >= greater than or equal to <= less than or equal to = equal to # not equal to
Conditions are often referred to as Boolean expressions, to distinguish them from arithmetic expressions. Arithmetic expressions produce values of type INTEGER and REAL and Boolean expressions produce values of type BOOLEAN.
Can you write an if-then-else example that reads an examination mark from the user and tells them whether they have passed or failed? You may assume that the pass mark is 40.
To OverviewTo Next Page