Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Teach Computer Science

KS3 Computing

Introduction to
algorithms

teachcomputerscience.com
2

Lesson Objectives

Students will learn about:


 Designing algorithms
 Various statements and structures used in pseudocodes
 Various symbols used in flowcharts

teachcomputerscience.com
1.
Content

teachcomputerscience.com
4

Introduction
 To program a computer, certain sets of logical instructions need to be
provided.
 Sets of logical instructions for a computer can be designed only when the
programmer thinks in the same way that a computer processes the
instructions.
 Computational thinking is a thought process involved in formulating a
problem and expressing its solution in such a way that computers can
effectively carry it out.

teachcomputerscience.com
“As soon as an Analytical Engine exists, it
will necessarily guide the future course of
the science. Whenever any result is
sought by its aid, the question will then
arise — by what course of calculation can
these results be arrived at by the machine
in the shortest time?”
-Charles Babbage, Passages from the life of a philosopher

5 teachcomputerscience.com
6

Software development
Developing a software is a complex process as it involves:
• Innovative thinking processes. It is quite difficult to create new software which
has not been done before.
• Complex testing procedures to check the functionality of software. Software
does not obey physical laws and to determine its properties, its developer
must test it under different conditions.
Software engineering describes structured methodology and techniques to
develop new software. Testing software is now made into a disciplined process
too.
teachcomputerscience.com
7

Computational thinking
 Computational thinking is beneficial in many ways and has changed
the way computer scientists look at the software development process
to be a more structured process.
 Using this, a complex problem is understood by breaking it into
various modules that can be further solved individually. The solution
obtained may involve a computer, a human, or both.

teachcomputerscience.com
8

Computational thinking
The components of computational thinking are:
 Abstraction
 Decomposition
 Algorithm design

teachcomputerscience.com
9

Algorithm
 An algorithm is generally written using pseudocode or flowcharts.
 Pseudocode is a readable description of what a computer program
will do.
 A flowchart depicts the steps and order to be followed to perform a
task.
 Designing a proper algorithm plays an important role in the software
development process.
teachcomputerscience.com
10

Designing an algorithm:
Robotic vacuum cleaner
 It moves forwards, backwards,
leftwards and rightwards to clean a
floor.
 A simple algorithm is given for
programming the movement of the
vacuum cleaner.

teachcomputerscience.com
11
Place robotic vacuum
cleaner at a corner of a
room Move forward 10 steps

Move forward 10 steps


Turn 90 degrees to right

Turn 90 degrees to left


Move forward a step

Move forward a step Turn 90 degrees to right

Turn 90 degrees to left Continues till the end of


room

teachcomputerscience.com
12

Refining an algorithm:
Robotic vacuum cleaner
 This algorithm can be made efficient
by using iteration.
 Iteration is the technique of repeating
a process.
 The motion of turning and moving 10
steps can be placed in an iteration.

teachcomputerscience.com
13
Place robotic vacuum
cleaner at a corner of a
room Move forward 10 steps

Move forward 10 steps


Turn 90 degrees to right

Turn 90 degrees to left


Move forward a step

Move forward a step Turn 90 degrees to right

Turn 90 degrees to left

Repeats
15 times

teachcomputerscience.com
14

Refining an algorithm:
Robotic vacuum cleaner
 A procedure is a list of sequences that
can be used several times in a
program.
 Programmers need not insert the
complete list of sequences.
 It is enough to call the procedure to
execute it in a program.
 The details of the procedure are
hidden from the user. This is called teachcomputerscience.com
procedural abstraction
15

Procedure for turning Procedure for turning


left: turn_left right: turn_right

Turn 90 degrees to left Turn 90 degrees to right

Move forward a step Move forward a step

Turn 90 degrees to left Turn 90 degrees to right

teachcomputerscience.com
16

Input and Output


Pseudocode Description
 Computer programs require input
from users. INPUT Name Value typed by user is stored
 INPUT is used for data entry. into variable ‘Name’
INPUT Price Value typed by user is stored
 PRINT is used to display a string or
into variable ‘Price’
a variable.
PRINT Name Displays the string stored in
Name
PRINT Price Displays the value stored in
Price
teachcomputerscience.com
17

Assigning a Value

 Values are assigned to a variable using the ← operator.


 The value on the right of the ← operator is assigned to the left.
 Mathematical expressions can be used on the right side of the ←
operator.

teachcomputerscience.com
18

Pseudocode Description

Assigning Name ← “Mike” Name has the value Mike

a Value Age ← 32 Age has the value 32

Gender ← “M” Gender has the value M

Salary ← 6000 Salary has the value 6000

Expense ← 3500 Expense has the value 3500

Saving ← Salary-Expense Saving has the value 2500

teachcomputerscience.com
19

Conditional statements

 Conditional statements are used when different actions need to be


performed based on different values of user input.

teachcomputerscience.com
20

Conditional statements
If… then… statement Case statements
IF condition CASE Choice OF
THEN PRINT “ Yes ” 1 : PRINT “You entered Choice 1”
ELSE PRINT ”No ” 2 : PRINT “You entered Choice 2”
ENDIF 3 : PRINT “You entered Choice 3”
OTHERWISE PRINT “Not a valid choice”
ENDCASE

teachcomputerscience.com
21

If… then… statement

Pseudocode Description

IF Weight < 85 • If the value stored in Weight is less


THEN PRINT “You can enter” than 85, then “you can enter” will be
ELSE PRINT “You are not allowed” displayed.
ENDIF • If the value stored in Weight is
greater than or equal to 85, then
“you are not allowed” will be
displayed.

teachcomputerscience.com
22

Operator Comparison
Operators for <  Less than
comparison >  Greater than
== Equal to
<= Less than or equal to
>= Greater than or equal to
!= Not equal to (in Python)
<>  Not equal to (in SQL)
() Group
AND Both
OR Either
NOT Complement
teachcomputerscience.com
23

If… then… with elseif


statement
Pseudocode Description

IF Weight >= 85 THEN • A person is not allowed if


PRINT “You are not allowed to enter” weight >= 85
ELSEIF Weight >= 75 THEN • Enters door 1 if
PRINT “Enter door 1” 75 <= weight < 85
ELSEIF Weight >=65 THEN • Enters door 2 if
PRINT “Enter door 2” 65 <= weight < 75
ELSE • Enters door 3 if
PRINT “Enter door 3” weight < 65
ENDIF
teachcomputerscience.com
24

Case statement
Pseudocode Description

CASE Choice OF • If the value is 1,


1 : PRINT “You entered Choice 1” “You entered Choice 1” will be
2 : PRINT “You entered Choice 2” displayed.
3 : PRINT “You entered Choice 3” • If the value is 2,
OTHERWISE PRINT “Not a valid choice” “You entered Choice 2” will be
ENDCASE displayed.
• If the value is 3,
“You entered Choice 1” will be
displayed.
• OTHERWISE is the path taken for all
other values
• ENDCASE denotes end of teachcomputerscience.com
the
statement
25

Loop statements

 Loop statements are used to perform a part of the algorithm multiple


times. The repetition of a set of lines is called iteration.

teachcomputerscience.com
26
Output Welcome
Welcome
Welcome
Welcome
Types of loop statements Welcome
Welcome
Welcome
Welcome
Welcome
FOR... TO… NEXT REPEAT... UNTIL WHILE… DO... Welcome
FOR Counter ← 0 TO 10 Counter ← 0 Counter ← 0
PRINT “Welcome” REPEAT WHILE Counter < 10 DO
NEXT PRINT “Welcome” PRINT “Welcome”
Counter ← Counter + 1 Counter ← Counter + 1
The value of counter
starts from 0 and is UNTIL Counter < 10 ENDWHILE
incremented in each
iteration. So, the
range of counter is 0 teachcomputerscience.com
to 9.
27

Flowchart symbols

Flowchart symbols are START END PROCESS


used to represent the
start and end of a
programme, process,
output and decision.
Yes
INPUT OUTPUT DECISION

No

teachcomputerscience.com
28

Algorithm
 A problem is broken into a series of logical steps called an
algorithm.
 For example: to design a program to multiply two numbers,
logical steps have to be provided. Repeated addition is the
widely used logic behind multiplication.

teachcomputerscience.com
29

Algorithm design
Flowchart Pseudocode
A flowchart depicts the steps and order Pseudocode is a method of representing
to be followed to perform a task. an algorithm using simple words and
mathematical operators.

teachcomputerscience.com
30

Flowchart: Example 1
 This flowchart checks whether a
number is odd or even.
 A decision box is used to check
whether the remainder of a
number divided by 2 is 0 or not.
 This decision box has one entry
point and two exit points.
 Sometimes a decision box may
have more than two exit points.
teachcomputerscience.com
31

Pseudo code:
Example 1
INPUT Num
IF Num MOD 2 = 0
THEN PRINT Num, “is an even number”
ELSE PRINT Num, “is an odd number”
ENDIF

teachcomputerscience.com
32

Pseudo code:
Example 2
An algorithm to print the numbers:
1 2 3 4 5 using loop statement is given.
Counter ← 1
WHILE Counter < 6 DO
PRINT Counter
Counter ← Counter + 1
ENDWHILE

teachcomputerscience.com
33

Let’s review some concepts


INPUT statement ← Symbol Conditional statements
INPUT statement is used for Values are assigned to a variable Conditional statements are used when
data entry. using the ← operator. different actions need to be
performed based on different values
PRINT statement
of user input.
PRINT statement is used to
Types: If… then… and case statements.
display a string or a variable.

Loop statements Types of loop statements Flowchart symbols

Loop statements are used to FOR... TO… NEXT Flowchart symbols are used to
represent the start and end of a
perform a part of the REPEAT... UNTIL
program, process, output and
algorithm multiple times. WHILE… DO... decision.

teachcomputerscience.com
2.
Activity

teachcomputerscience.com
35

Activity-1
Duration: 20 minutes

1. Create a refined algorithm to draw a square with sides a length


of
6 cm. Specify both pseudocode and flowchart for the same.

teachcomputerscience.com
36

Activity-2
Duration: 15 minutes

1. A theme park allows children to enter an activity only if they are at


least 5 years old. Also, the height of children entering that activity
should be 105 cm and above. Create an algorithm to check the
age and height of children entering this activity. Specify the
pseudocode for the same.

teachcomputerscience.com
3.
End of topic questions

teachcomputerscience.com
38

End of topic questions


1. What are input and output statements? Give examples.
2. What operator is used to assign values to variables?
3. How are mathematical expressions used in statements assigning
values?
4. What are the different conditional statements?
5. What is iteration? What are the different iteration statements?
6. How is a repeat…until… loop different from while…do… loop?

teachcomputerscience.com
39

End of topic questions


8. Write an algorithm using loop statements to print the numbers:
5 4 3 2 1.
9. Create a flowchart for the algorithm specified in answer 8.

teachcomputerscience.com

You might also like