Computational Thinking
and Problem Solving
(SE1101)
Lecture 2:
Logical and algorithmic
thinking
2023
Chapter 2 goal
Getting you into the habit of thinking logically and algorithmically
Picture adapted from: https://www.technokids.com/blog/programming/its-easy-to-improve-logical-thinking-with-programming/
Logical and Algorithmic Thinking
• Logic and algorithms are essential to CT.
• Each has its own set of rules, procedures and definitions, which are
very precise and systematic.
• By the end of the chapter, you will have learned how to apply logic and
algorithms to problem-solving.
Outline
• Logical thinking
• Boolean logic
• Symbolic logic
• Logical operators
• Algorithmic thinking
• Algorithm
• Controlling algorithm execution
• Faulty assumptions about logic and algorithms
Logical thinking
Logic
Logic is a system used for distinguishing between correct and
incorrect arguments
allows using known knowledge to arrive at some further conclusions
e.g.
a computer is used to automate • Socrates is a man.
reasoning • All men are mortal.
• Therefore, Socrates is mortal
Logic
• Socrates is a man.
argument • All men are mortal.
• Therefore, Socrates is mortal
a chain of reasoning that ends up in a conclusion
Logic
• Socrates is a man.
premise
• All men are mortal.
• Therefore, Socrates is mortal
like any ordinary statement, except that it can be
evaluated to obtain an answer of ‘true’ or ‘false’
Logic
• Break a leg!’
premise?
• ‘What time is it?’,
can’t be premises to an argument.
It’s neither true nor false to say
Boolean logic
• a form of logic that deals with statements having one of only two values:
true (1) or false (0)
• statements in Boolean logic are also known as propositions
• properties of proposition:
• can only have one value at any one time
• must have clear and unambiguous meaning
• can be compound to make more complex ones ( called compound
propositions using logical Operators)
Symbolic logic
• using symbols instead of natural language sentences
• e.g.
• P = at least one square on the board is still empty
• Q = neither player has achieved a row
• S = the game is still in progress
then we can say:
If P and Q, then S
Logical operators
Logical operators
P Q P AND Q P OR Q NOT P P IMPLIES Q Q IF AND ONLY
IF P
T T T T F T T
T F F T F F F
F T F T T T F
F F F F T T T
Algorithmic thinking
Algorithm
a sequence of clearly defined steps that describe a process to follow a
finite set of unambiguous instructions with clear start and end points
a correct algorithm is the ultimate basis of any computer-based
solution
Algorithm
can be expressed in many different notations, including flowchart,
and pseudocode:
Pseudocode is an informal means of writing an algorithm.
A flowchart is a graphical representation that uses graphic symbols
and arrows to express algorithms.
Algorithm
• pseudocode • flowchart
PROGRAM Add:
a = 10
b = 20
sum = a + b
PRINT sum
END.
Defining algorithms
• collection of individual steps
• definiteness (i.e. precisely defined)
• precisely defined (i.e. carried out in the order specified)
Variables
n only a single step can be under consideration at any one time
n once a step has been executed, the computer forgets all about it and
moves on to the next
n if we want the computer to remember the result of a step for later use, we
have to explicitly tell the computer to do so
n algorithms provide for this by means of variables
Variables
• a variable is a placeholder for important
information the computer should keep note of.
• it can also have its values updated throughout an
algorithm’s execution (this is called assignment)
Controlling algorithm execution: Selection
• creating a point at which the computer has to decide between
performing a set of steps or not
Controlling algorithm execution: Iteration (looping)
Five little ducks went swimming one day
Over the hill and far away
Mother Duck said, "Quack, Quack, Quack"
But only four little ducks came back
One, two, three
Four little ducks went swimming one day
Over the hill and far away
Mother Duck said, "Quack, Quack, Quack"
But only three little ducks came back
One, two
…..
One little duck went swimming one day
Over the hill and far away
Mother Duck said, "Quack, Quack, Quack"
But no little ducks came swimming back
Controlling algorithm execution: Iteration (looping)
• allows repeating a series of steps over and over, without the need to write
out each individual step manually
variables is used to
control the execution of
an algorithm
Controlling algorithm execution: Iteration (looping)
do something a certain do something so long as the
number of times condition specified is true
e.g. ‘repeat verse 99 e.g. repeat the verse until
times’ you master it
Controlling algorithm execution
• e.g. game of noughts and crosses
Faulty assumptions about logic and algorithms
The need for clarity and meticulousness
A computer will do exactly as it is told. If it is told division by zero
to do something impossible, it will crash. infinite loop
A computer has no innate intelligence of its own.
It will not do anything that it has not been washing ingredients before cooking
instructed to do.
A computer has no common sense. It will not try
to interpret your instructions in different ways. It
following ambiguous recipe
will not make assumptions or fill in the obvious
blanks in an incomplete algorithm.
Incorrect use of logical operators
• Computers interpretate logical operators differently than human.
• e.g.
• Everyone whose surname begins with A and B is assigned to Group 1.*
• The player took their turn and the game was finished.
*might seem logical to a human, but to a computer, which strictly follows the logical meaning of
AND, it’s illogical. That’s because, as written, the statement tests two things: does a surname
begin with A and does it begin with B? However, no surname can have different first letters at the
same time. You can ask a computer to carry out this test as much as you like, but it will always fail.
Missing certain eventualities
if-then
• if score is greater than 50 per cent,
• then mark student’s grade as a pass.
?
Missing certain eventualities
if-then-else
• if score is greater than 50 per cent,
• then mark student’s grade as a pass,
• else mark student’s grade as a fail.
Complex conditionals
• if score is not less than 51 per cent or greater
than 80 per cent,
• then mark student’s grade as an ordinary pass
• …..
Order of precedence: Mathematical
1. parentheses
2. exponents
3. division and multiplication
4. addition and subtraction
e.g.
• 9−3×4
• (9−3)×4
Order of precedence: Logical
1. parentheses
2. not
3. logical AND
4. logical OR
Order of precedence: Logical
Assume score = 55 Assume score = 85
• if ¬score < 51 V score > 80 • if ¬(score < 51) V (score > 80)
• if ¬(false) V (score > 80) • if ¬(false) V (score > 80)
• if true V (score > 80) • if true V (score > 80)
• if true V false • if true V true
• true • true
Any Question ?
Thank you!