Pseudocode (Assignment, Conditional, Iteration)
Pseudocode (Assignment, Conditional, Iteration)
Pseudocode (Assignment, Conditional, Iteration)
Definitions
Logical Operators
AND ,OR, NOT
CONDITIONAL STATEMENT
Task 1
CREATING PSEUDOCODE
Pseudocode
Using pseudocode is a clear and
concise way to represent an
algorithm.
Radius (R) ← 4 cm
∏ ←3.14
Circumference of a circle (C) ← 2*∏ *R Solve these small
Pseudocode
Name ← “Alex” examples
Message ← “Hi,How are you” + Name
A ← 20
B ← 30
C ← 40
Total ← A +B+C
IF –ELSE and CASE
IF STATEMENT USING MORE THAN ONE
CONDITION
Nested Loop & To edit / improve Pseudocode
The algorithm below checks if a percentage mark is valid and a pass or a fail. This makes use of two IF statements. The second IF statement is
part of the ELSE path of the first IF statement. This is called a nested IF.
Read the scenario
The scenario given the time taken by a runner in marathon in seconds
7
DECLARE answer : string
PRINT ”Enter the input”
INPUT answer
IF answer=“chocolate” THEN
PRINT ”Yum”
ELSE
IF answer=“Biscuits” THEN
PRINT ”Crunchy”
ELSE
IF answer=“Sweet” THEN
PRINT ”chewy”
ELSE
PRINT ”I don’t know what that means”
ENDIF
ENDIF
ENDIF
* Task 3
Total=Total +Mark
counter=counter+1
Iterations
Loop Conditions
In general, statements are
executed sequentially: The first
statement in a function is
executed first, followed by the
second, and so on. There may
be a situation when you need to
execute a block of code several
number of times.
Walt: To write pseudocode using
Iterative statements
Types of Iteration
Loop type Description When it should be used
WHILE loop Repeats a section of code while The number of iteration is not known. If
the control condition is true condition is false, it will not be executed.
REPEAT UNTIL loop Repeats a section of code until The number of iteration is not known. The
the control condition is true condition is checked after code is
executed, so code will run atleast once.
For loop
• Element:-
• For:-The start of the loop
• i= 0 to 10
• i is a counter that records the number of iterations that have been run
• Range takes three arguments:-
– The integer to start at (default=0)
– The first integer to exclude(required)
– The amount to increment by (default=1)
Output:- [0,1,2,3,4,5,6,7,8,9]
For Loop Example
For loop executes a sequence of statement multiple time
and abbreviates the code that manages the loop variable.
Try it!!
Write a pseudocode to print first 10 natural numbers
DECLARE i: integer
FOR i=1 to 11
PRINT i
NEXT i
Write a pseudocode to print the first 10 even numbers
DECLARE i: integer
FOR i=0 to 20
IF i MOD 2=0 THEN
PRINT i
ENDIF
NEXT i
Activity (Self assessment)
Task 1: Write a pseudocode to output Extension Task
the sum of first 10 even numbers.
Write a pseudocode to accept 10 numbers
Task 2: Write a pseudocode to accept 10 and print the largest number.
numbers display the count of odd and
even numbers in the input.
DECLARE i, s: integer
s=0
FOR i=0 to 20
IF i MOD 2=0 THEN
s=s + i
ENDIF
NEXT i
PRINT ”Sum is “,s
Activity (Self assessment)
Task 2