CS Simplified
Algorithm in
pseudocode
teachcomputerscience.com
2
Lesson Objectives
Students will learn about:
▪ Role of algorithms in programming
▪ Various statements and structures used in pseudocodes
▪ Various symbols used in flowcharts
teachcomputerscience.com
1.
Content
teachcomputerscience.com
4
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
5
Input and Output
Pseudocode Description
▪ Computer programs require input
from users. INPUT Name Value typed by user is stored
into variable ‘Name’
▪ INPUT is used for data entry.
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
6
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
7
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
8
Conditional statements
▪ Conditional statements are used when different actions need to be
performed based on different values of user input.
teachcomputerscience.com
9
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
10
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
11
Operator Comparison
< Less than
Operators for
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
12
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
13
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 the
statement
teachcomputerscience.com
14
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
15
Output Welcome
Welcome
Welcome
Welcome
Types of loop statements Welcome
Welcome
Welcome
Welcome
Welcome
FOR... TO… NEXT REPEAT... UNTIL WHILE… DO... Welcome
FOR Counter ← 1 TO 10 Counter ← 0 Counter ← 0
PRINT “Welcome” REPEAT WHILE Counter < 10 DO
NEXT PRINT “Welcome” PRINT “Welcome”
Counter ← Counter + 1 Counter ← Counter + 1
UNTIL Counter < 10 ENDWHILE
teachcomputerscience.com
16
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
17
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
perform a part of the represent the start and end of a
REPEAT... UNTIL
program, process, output and
algorithm multiple times. WHILE… DO... decision.
teachcomputerscience.com
2.
Activity
teachcomputerscience.com
19
Activity-1
Duration: 20 minutes
1. Software is designed to calculate Marks (%) Grade
grades of students according to the 90-100 A*
marks scored. The grades for marks 80-89 A
scored are given in the table. 70-79 B
Create the pseudocode and 60-69 C
flowchart of the algorithm. 50-59 D
Below 50 Fail
teachcomputerscience.com
20
Activity-2
Duration: 15 minutes
1. Create a flowchart and pseudocode for an algorithm to calculate
factorial of a number.
teachcomputerscience.com
3.
End of topic questions
teachcomputerscience.com
22
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