4.1.4-4.1.8 Thinking logically
This is one of the fundamental parts of computational thinking. Computers process data logically but this is not “thinking”. In order for the computer to work it needs to be given instructions. In order for these instructions to work then the programmer must think logically in order to design the program. Logical thinking is also fun and there are a great many ways for you to develop your logical thinking skills such as Suduko[1] and Kakuro[2] or Lightbot[3].
4.1.4 Identify when decision-making is required in a specified situation.
In many cases the steps required to solve a problem such as the example given above require no decision making and are linear solutions. However if we may have simple situations that require a decision to be made and there are two alternative decisions to be made.
EG A system that automatically grades exam papers. Part of the program will involve a decision. This can be expressed using a flowchart:
Or alternatively in pseudocode:
if GRADE>50 then
Output "Pass"
else
Output "Fail"
Endif
In this case the decisions that need to identified in order to solve the problem is the logical condition determining if the exam is a pass or fail.
4.1.6 Identify the condition associated with a given decision in a specified problem.
Boolean logic
Boolean logic is named after George Boole, a famous 19th Century mathematician. In this logic all values are expressed as either true or false. This fits well with binary systems which can be expressed as either 0 or 1.
Boolean operators exist that can be used to manipulate the basic true false values. Examples of operators are as follows
Operator | Meaning |
= | Is equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
≠ | Not equal to |
AND | The output is true if both inputs are true |
OR | The output is true if one of the inputs is true |
NOT | The output is true if the input is false |
NAND | The output is true if either or both inputs are false. |
NOR | The output is true if both inputs are false |
XOR | The output is true if only one of the inputs is true and only one is false. Known as exclusive OR. |
These may be expressed in a logic table where 0 is FALSE and 1 is TRUE.
AND
Input A | Input B | Output Z |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
OR
Input A | Input B | Output Z |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
NOT
Input A | Output Z |
0 | 1 |
1 | 0 |
NAND
Input A | Input B | Output Z |
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
NOR
Input A | Input B | Output Z |
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 0 |
XOR
Input A | Input B | Output Z |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
These can be used to create logic table to find the solution to problems. For example a student is only allowed to play computer games if their homework is done and their room is tidy. The solution to this can be illustrated in a logic table as follows:
Homework done | Room tidy | Output Z |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
More details of how to solve logic problems is covered in Section 2.1.11.
Iteration and testing conditions
Iteration in computing is the repetition of a block of statements within a computer program. In practice this repetition can take place a set number of times or can repeat based on a set of logical criteria that change over time.
There are two main ways this is achieved in programming which are a for loop and a while loop.
For loop
In the IB pseudocode document the syntax for a for loop is a from to loop. In most programming languages the syntax of a for loop is
for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
{
// Code for the for-loop's body goes here.
}
This may be expressed in a flowchart as:
In JAVA the syntax would be
For (int x= 0; x<5;x++){
//do something
}
In IB psueudocode it would be as follows:
loop X from 0 to 4
//do something
end loop
While loop
A while loop is an iteration but in this case the loop must repeat until a certain condition is met. This can be programmed in JAVA in the following way
while (COUNT<5) {
COUNT = COUNT + 1
}
In IB pseudocode this would look like this:
loop while COUNT<5
COUNT = COUNT +1
end loop
A variation of the while loop is the do while loop in which a condition is first run before the check is made. The difference is that the evaluation of the expression is done at the end of the loop:
do {
//Statements
}
4.1.7 Explain the relationship between the decisions and conditions of a system.
In computing conditional statements are features which perform different computations depending on whether a boolean expression evaluates as true or false. The basic structure of this statements using IB pseudocode is as follows:
if (condition) then
//Do something
else
//Do something else
end if
This can be used in a number of contexts in computer programming.
4.1.8 Deduce logical rules for real-world situations.
Thinking ahead
Thinking concurrently
Thinking abstractly
Create A folder within the htdocs folder as in example below which will store your php programs
Exercise A Pseudo Code
Convert the following segments of pseudo code into a programming language of your choice
A = 5
B = 10
if A < B then
output A, " is less than ", B
else
output A, " is greater than ", B
end if
COUNT = 0
loop while COUNT < 20
output COUNT
COUNT = COUNT + 2
end loop
STR1 = "red"
STR2 = "blue"
if NOT (STR1 == STR2) then
STR1 = "blue"
end if
output STR1
COUNT = 0
SUM = 0
loop until COUNT = 10
SUM = SUM + COUNT
COUNT = COUNT + 1
end loop
loop X from 1 to 10
if X mod 2 = 0 then
output "even"
else
output "odd"
end if
end loop
loop X from 0 to ARRAY.length - 1
if ARRAY[X] > ARRAY[X + 1] then
TEMP = ARRAY[X + 1]
ARRAY[X + 1] = ARRAY[X]
ARRAY[X] = TEMP
end if
end loop
Exercise B Flow Charts
Convert the following into Pseudo Code
Exercise C Pseudo Code
1 Determine if two numbers are equal. If they are equal print "same" otherwise print "different".
2 Step thru an un-ordered array containing integers and print out the largest and smallest numbers
Trace Tables
Example 1
This algorithm calculates the second hand price of different models of car. The variable condition is an integer between 1 and 4 where 1 is excellent and 4 is very bad.
Example 2
Collections
By copying the table below, trace the following algorithm using the data in the collection DATA. Note: B and C are also collections and are initially empty.