Pseudocode
Pseudocode
Learning Session
Grade 7
1
Three basic things
• Sequence
• Selection
• Iteration/Repetition
2
Sequence
• Variable: Value can be changeable
Real life example: Your weight, Height…
• Constant: Value should be fixed DECLARE id, pass: STRING
Real life example: Your name, Father’s name, Value of Pi…
Declaration Before assign any variable you have to declare it to our system. OUTPUT “ID:”
Real life example: If you want to login your email account & provide user INPUT id
id :1234. It will not accept as in system it is declared you have to use –
Alphabet, @ … OUTPUT “Pass:”
INPUT pass
Initialization It will assign a space to take input from user.
Real life example: Provide a space so that user
ID: can give id
Pass:
3
Lesson 01,
Variable: Datatype
There are five datatypes: INTEGER,REAL, CHAR, STRING ,BOOLEAN
We declare the data type so that system can understand which type of data they can take for which INPUT
4
H.W:
Write the pseudocode at your copy.
1.Let's see how to output the classic "Hello World" to the terminal/console.
2. Ask the user to enter 3 bits of information of various data types, then output a sentence to the user - e.g.
•name
•age
•height
An example sentence could then be "Dumbledore is 715 years old and is 1.85m tall“
3. For different levels of formality, we often want to greet people in different ways - have the user enter their title
(Dr, Mr, Mrs, Miss etc), first name & last name, then display greetings in various levels of formality - e.g.
•Dear Lord Riddle
•Hi Tom
•Signed: Riddle, Tom
5
Lesson 02,
Operator: For doing mathematical & logical operation we need
operators.
We will use three types of operators. They are:
1. Arithmetic :+,-,*,/,^,MOD, DIV
2. Logical : =,>,<, >=,<=,<>
3. Boolean: AND, OR, NOT
Let's see the 7 arithmetic operators in IG - note that DIV and MOD are functions for IG
+ : addition
- : subtraction
* : multiplication
/ : division
^ : exponentiation (power)
DIV : returns the quotient of one number divided by another - i.e. how many times the number can be wholly divided by the other
MOD : returns the remainder after the integer division (DIV)
People often get confused about DIV and MOD - imagine sharing 14 slices of pizza among 5 people:
6
Lesson 03
Operator: Logical Operator Operator: BOOLEAN Operator
7
SELECTION:
1. IF… THEN… ELSE…
2. CASE OF… OTHERWISE…
•DECLARE a, b : INTEGER
•
a <-- 5
•b <-- 4
•
IF a > b THEN
• OUTPUT "a is bigger than b"
•ELSE
• OUTPUT "b is bigger than a"
•ENDIF
Exercise:
1. Write a pseudocode that input marks of students an print pass or fail. Passing mark is 50.(C.W)
2. Pseudocode that input a number & print the number is positive or negative.(C.W)
3. Input two numbers from user & find out the greatest one among them.(C.W)
8
Nested IFs
• DECLARE a, b, c : INTEGER
•
Exercise: Program to check positive, negative or zero using simple
a <-- 6
• b <-- 3
• c <-- 8
• Syntax:
IF a > b AND a > c THEN
IF Condition
• OUTPUT "a is bigger than b and c"
Else
• ELSE
• IF a = b AND b = c THEN
• ELSE
• ENDIF
• ENDIF
• ENDIF
• ENDIF 9
Lesson 04, 5-2-25
CASE Statement:
• Syntax:
CASE OF ______________
_____: OUTPUT ”________”
_____: OUTPUT ”________”
………………………………………
OTHERWISE : OUTPUT ”________”
Now write a program to give the stage for age as 1-4 is toddler, 5-12 is
school age,13 to 19 is teenager & <19 is adult. No negative value is
acceptable.
• DECLARE age : INTEGER
• OUTPUT "Enter your age: "
• INPUT age
• CASE OF age
• 1,2: OUTPUT "Child"
• 3,4: OUTPUT "Toodler"
• 5 TO 12 : OUTPUT "School-age"
• 13 TO 19: OUTPUT "Teenage“
• OTHERWISE: IF age>19 THEN
• OUTPUT "Adult“
• ELSE OUTPUT "Negative value is not
acceptable"
• ENDIF
• ENDCAS
Lesson 5 13-02-25
Practice Work:
4. Input three numbers from user & find out the greatest one among them.
5. Write a Pseudo code that input a number from user & print that number is even or odd.
6. Pseudocode that input amount in ATM Machine & print ‘accept’ if amount is multiple of
500. Otherwise print ‘not accepted’.
7. Write a pseudo code that input a character & print input character is vowel or not vowel.
8. Write a pseudocode that input two numbers & an operator (+,-,*,/) & print the answer
after applying function of user given operator.
9. Write a pseudocode that input grade of a student & print remarks.
Very good at A or A*
Good at B
Fair at C
Need improvement at D or E
Retake at U
Print invalid grade if grade is outside of given grade.
12