Chapter 2: Conditions and Control Flow

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.

  1. Insert your debit card.
  2. Choose the withdraw option.
  3. If desired, enter a specific value, go to 5.
  4. Select one of the pre-set values.
  5. If desired, choose the denomination.
  6. Enter your PIN, if correct go to 8.
  7. Go to 6.
  8. Take your card and money.

However, we have not yet learned how to do it in our programs, in the simple examples that we have written until now, we have executed every step sequentially.
In this chapter we will talk about conditional execution of certains parts of a program.

Contents

  1. Truth Values
  2. Comparison Operators
  3. Boolean Algebra
  4. Control Structures
  5. If Statement
  6. Loop Statement
  7. Waiting for User Input


  1. Truth Values

  2. 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.

    Back to top



  3. Comparison Operators
  4. 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.

    Back to top



  5. Boolean Algebra
  6. 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:

    1. not : negates the expression
    2. and : the expression is true if both sides of it are true, false otherwise
    3. or : the expression is true if at least one side is true, false only when both sides are false
    AND true false
    true true false
    false false false
    OR true false
    true true true
    false true false

    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.

    Back to top



  7. Control Structures
  8. 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:

    1. Increment the value of x by 1
    2. Print value of x
    3. If value of x equals 10, goto 5
    4. Go to 1
    5. End of the algorithm

    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.

    Back to top



  9. If Statement
  10. 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.

    Back to top



  11. Loop Statement
  12. 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:

    Back to top



  13. Waiting for User Input
  14. 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:

    Back to top