Pseudocode (Assignment, Conditional, Iteration)

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

CHAPTER 7

Definitions

Algorithm: An algorithm is a step by step sequence of solving a given problem.


Pseudocode: An informal way of writing a program. It only represents the algorithm of the
program in natural language and mathematical notations.
Flowchart: A flowchart represents an algorithm using a diagram.
Tracetable: A trace table is a technique used to test an algorithm and predict step by step how
the computer will run the algorithm.
PSEUDOCODE
DATA TYPES
ASSIGNMENT STATEMENTS
Conditional statement
Mathematical Operators Relational Operators

Logical Operators
AND ,OR, NOT
CONDITIONAL STATEMENT
Task 1
CREATING PSEUDOCODE
Pseudocode
Using pseudocode is a clear and
concise way to represent an
algorithm.

Data items to be processed by the


Rules For Pseudocode
algorithm are given meaningful
1. Write only one statement per
names in the same way that
line
variables and constants are in a
2. Capitalize initial keyword
high-level programming language.
3. Indent to show hierarchy
4. End multiline structures
Pseudocode is not bound by the
5. Keep statements language
strict syntax rules of a
independent
programming language.
What values will the following variables have after assignments have been completed?

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

Create an algorithm to calculate the total time


Task 2

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

FOR loop Repeat a section of code a The number of iteration is known


predetermined no of times

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.

Task 3:. Write a pseudocode to input 10


numbers and output the average
Activity (Self assessment)
Task 1
Write a pseudocode to output the sum of first
10 even numbers

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

Write a pseudocode to accept 10 numbers


display the count of odd and even numbers in
the input.
Activity (Self assessment)
Task 3

Write a pseudocode to input 10 marks and


output the average
Activity (Self assessment)
Iteration–Challenging Task
Iteration–Challenging Task
DECLARE num1 ,num2 ,answer :integer
PRINT ”Enter two numbers”
INPUT num1,num2
answer=num1*num2
PRINT “Answer is “, answer
DECLARE number : integer
PRINT “Enter the number”
IF number <> 5 OR number<>6 THEN
PRINT ”The number you have entered is not 5 or 6”
ENDIF
DECLARE number: integer
PRINT “Enter a number”
INPUT number
IF number>0 and number<10 THEN
PRINT “blue”
ELSE
IF number>10 and number<20 THEN
PRINT ”red”
ELSE
IF number>20 and number<30 THEN
PRINT ”green”
ELSE
PRINT “It is not the correct color option”
ENDIF
ENDIF
ENDIF
DECLARE i : integer
FOR i = 0 to 101
IF i mod 5=0 THEN
PRINT i
ENDIF
NEXT i
DECLARE counter, mark: integer
counter=1
While counter<=10 Do
PRINT “Enter exam mark”
INPUT mark
IF mark > 40 THEN
PRINT “You have passed”
ELSE
PRINT” You have not reached the pass mark”
ENDIF
counter=counter+1
ENDWHILE
DECLARE max, number, i: integer
max=0, i=1
REPEAT
PRINT “Enter Number”
INPUT number
IF number > max THEN
max number
ENDIF
i=i+1
UNTIL i<=5
PRINT ”Largest is “,max

You might also like