0% found this document useful (0 votes)
10 views

12.04-12.06 Variables and Logic Statements

The document discusses variables in programming and how they are used to store and manipulate data. Variables have identifiers and can be assigned values, updated, copied between each other, and swapped. Conditional logic and relational operators allow programming different execution paths based on conditions being true or false.

Uploaded by

q8hqtd5n2h
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

12.04-12.06 Variables and Logic Statements

The document discusses variables in programming and how they are used to store and manipulate data. Variables have identifiers and can be assigned values, updated, copied between each other, and swapped. Conditional logic and relational operators allow programming different execution paths based on conditions being true or false.

Uploaded by

q8hqtd5n2h
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Variables

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

Copying a value - Values can be copied from one variable to another.


The following pseudocode takes the value stored in Value1 and copies it to Value2.
Value2 ← Value1

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

Using pseudocode we write:


Temp ← Value1
Value1 ← Value2
Value2 ← Temp
Logic statements
IF there are engineering works on the Victoria Line
THEN
Take the Piccadilly Line to Green Park (6 stations) Take the Jubilee Line to Westminster (1
station)
ELSE
Take the Victoria Line to Green Park (4 stations) Take the Jubilee Line to Westminster (1
station)

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.

If Age < 13 then person is a child.


If Age > 19 then person is an adult.
If Age >= 13 AND Age <= 19 then person is a teenager.

A number-guessing game follows different steps depending on certain conditions.

- The player inputs a number to guess the secret number stored.


- If the guess was correct, output a congratulations message.
- If the number input was larger than the secret number, output message “secret
number is smaller”.
- If the number input was smaller than the secret number, output message “secret
number is greater”.

We can re-write the number-guessing game steps as an algorithm in pseudocode:


SET value for secret number
INPUT Guess
IF Guess = SecretNumber
THEN
OUTPUT "Well done. You have guessed the secret number"
ELSE
IF Guess > SecretNumber
THEN
OUTPUT "secret number is smaller"
ELSE
OUTPUT "secret number is greater"
ENDIF ENDIF

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.

- Only two variables are used.


- The conditional statements are not nested and do not have an ELSE part. This
makes them easier to understand.
- This algorithm can be adapted more easily if further numbers are to be compared.

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.

You might also like