In the example of an ATM money withdrawal algorithm in the previous chapter we have seen steps, where we would jump to other steps depending on a specific condition.
Also when we were discussing values and data types we have already seen an examplary use of a truth value.
Truth or logical values are used in programming to evaluate a specific condition and determine whether it is true or false.
Therefore, there are only two possible truth values: true and false.
Now that we have defined our logical values, we can use them to actually check whether some conditions are true or not.
To do this, we can use conditional operators:
For example, 2 + 2 = 4 will yield true while 2 + 2 /= 4 will yield false.
Sometimes we have to check more than one condition, for example we want to check if two numbers a and b are equal and are greater than 0.
To do this, we have to define at least two conditions: a = b and a > 0.
Right now, we have two separate expressions, however both must true in our scenario, we have to combine them using a logical operator and: a = b and a > 0.
Now we have one logical expression, which is only then true, if both subexpressions are also true.
To sum it up, we have three logical operators, ordered according to their precedence:
|
|
The precedence is important since it determines in which sequence the operators are evaluated.
Similar to how multiplication or division is evaluated before addition or subtraction, the not operator is always applied first.
Afterwards, the operator and is evaluated before the operator or.
For example, in the expression x < 0 and x = -5 or x > 0 and x = 5 we would first evaluate the expressions x < 0 and x = -5 and x > 0 and x = 5.
Once done, we would compare the results with the or operator
To get a feeling for the boolean algebra, copy the example below and play around with it assigning different values to x or defining new expressions.
As already mentioned at the beginning of this chapter, until now we were executing our programs in a purely sequential manner.
We have also said that, depending on specific conditions, which we now know how to define using logical values and boolean algebra, we can skip or repeat certain steps.
Thus we have three concepts to define the flow of our program:
As you can see, in the last example we would have an endless repetition, i.e. we would eternally increment our x and print its value never reaching the end.
To prevent such situations, we have to define termination conditions for our repetitions:
This imprived version of our algorithm would repeat the required steps until x reaches the value of 10, in which case the algorithm will terminate.
It should be noted that modern high level programming languages no longer use a goto instruction, instead they rely on conditional statements, loops and recursion.
We will handle recursion in the last chapter, for now, let us concentrate on conditional statements and loops.
If statements allow us to check a condition and execute certain lines of code is this condition is met:
The line of code between then and endif will only be executed in the value of x is 5, which is true in this case.
If statements also allow us to define several branches, in fact we can define as many branches as we like.
We can also nest our if statements, allowing us to implement more complex branching.
By now we can start implementing a simple logic for our ATM money withdrawal algorithm.
We want a simple program where we can define our account balance, enter the amount of money to withdraw and check if we can actually withdraw that amount.
If we can, the account balance should be reduced by the respective amount.
The final control structure is repetition.
This can be achieven either via recursion, which we will discuss in the last chapter, or by using loops.
Most modern programming languages that support loops offer various kinds of loops which allow to specify their own termination conditions.
MuLE, on the other hand, offers a single loop construct, which is by definition infinite.
That means, that we have to use an if statement to check the termination condition.
The following example demonstrates a simple loop:
The produced output is:
In the previous example we have demonstrated a loop, which runs a set amount of time.
However, sometimes we do not know how often we want to execute certain lines of code.
For example, if we want to allow our algorithm to wait for user input and allow different actions depending on that input.
We can do that with a loop, which may run for a potentially infinite number of times.
The program will terminate only if we enter n or N.
Let us now expand our previous simple ATM example.
We want to display our current account balance, and offer three options: terminate the program, transfer money to the account or withdraw money.
The user interaction could look like this: