02 - Logical and Algorithmic
02 - Logical and Algorithmic
2023
Chapter 2 goal
• 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
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
• Socrates is a man.
premise
• All men are mortal.
• Therefore, Socrates is mortal
• Break a leg!’
premise?
• ‘What time is it?’,
• 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
If P and Q, then S
Logical operators
Logical operators
• pseudocode • flowchart
PROGRAM Add:
a = 10
b = 20
sum = a + b
PRINT sum
END.
Defining algorithms
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
• 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)
*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
?
Missing certain eventualities
if-then-else
• …..
Order of precedence: Mathematical
1. parentheses
2. exponents
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