12.04-12.06 Variables and Logic Statements
12.04-12.06 Variables and Logic Statements
When inputing data for a process, individual values need to be stored in memory. Specific
memory location need to be reffered to so that we can write statements of what to do with
the value stored there. We refer to these as variables. You can imagine these variables like
boxes with name labels on them. When a value is input, it is stored in the box with the
specified name (identifier) on it. E.g, the variable used to store a count of how many guesses
have been made in a number guessing game might be given the identifier
NumberOfGuesses and the player’s name might be stored in a variable called ThisPlayer, as
shown below:
Variable identifiers should not contain spaces, only letters, digits and _. To make algorithms
easier to understand, the naming of a variable should reflect the variable’s use - more than
one word is used as an identifier. The formatting convention used here is known as
CamelCaps - makes an identifier easier to read.
Assignments
Assigning a value - The following pseudocode stores the value that is input (for e.g 15) in a
variable with the identifier. INPUT Number
The following pseudocode stores the value 1 in the variable with the identifier
NumberOfGuesses. NumberOfGuesses ← 1
Updating a value - The following pseudocode takes the value stored in NumberOfGuesses,
adds 1 to that value and then stores the new value back into the variable
NumberOfGuesses. NumberOfGuesses ← NumberOfGuesses + 1
The value in Value1 remains the same until it is assigned a different value.
Swapping two values - If we want to swap the contents of two variables, we need to store
one of the values in another variable temporarily. Otherwise the second value to be moved
will be overwritten by the first value to be moved.
In (a), we copy the content from Value1 into a temporary variable called Temp. Then we
copy the content from Value2 into Value1 (b). Finally, we can copy the value from Temp into
Value2 (c).
The selection construct in Table 12.01 uses a condition to follow either the first group of
steps or the second group of steps.
A condition consists of at least one logic proposition. Logic propositions use the relational
(comparison) operators
Conditions are either TRUE or FALSE. In pseudocode, we distinguish between the relational
operator = (which tests for equality) and the assignment symbol ←.
A person is classed as a child if they are under 13 and as an adult if they are over 19. If they
are between 13 and 19 inclusive they are classed as teenagers. We can write these
statements as logic statements.
More complex conditions can be formed by using the logical operators AND, OR and NOT.
E.g, the number-guessing game might allow the player multiple guesses; if the player has
not guessed the secret number after 10 guesses, a different message is output.
There are several advantages of using the method in Worked Example 12.03 compared to
the method in Worked Example 12.02.
The disadvantage of the method in Worked Example 12.03 compared to the method in
Worked Example 12.02 is that there is more work involved with this algorithm. If the second
number is bigger than the first number, the value of BiggestSoFar has to be changed. If the
third number is bigger than the value in BiggestSoFar then the value of BiggestSoFar has to
be changed again. Depending on the input values, this could result in two extra assignment
instructions being carried out.