Pseudocode by MR Saem
Pseudocode by MR Saem
3. The names of the variables/identifiers are written in lowercase with the first
character of the name being a letter and in uppercase.
INPUT Height_of _student
OUTPUT TotalAmount
IF User_turns > 4
Assignment/ Storage
In order to assign a value to a variable we use the following symbol also called the
assignment operator .
Variable_name Value
Or
Variable_name Variable_name
For example
Age 20
Day “Monday”
Tax Price × 0.25
Squared_value Value ^ 2
Profit Sale_price – Purchase_price
Counting
The concept of counting is used when it is required to keep the count of a certain
object or criteria. For example, increasing the customer number by 1 at the
checkout counter of a mall.
Count Count + 1
Count Count – 1
Totalling
This concept is used when we are keeping a total of different amounts being
entered. For example, to keep the total sum of profit made on different days of the
week:
Total_amount Total_amount + profit
Total_bill Total_bill + Item_price
Before counting or making a total, always set the value of these variables to
0 at the start.
Or
OUTPUT “Total amount is: ”, Total_amount
The comma is used as a separator between the message and the variable name.
Example
Write pseudocode for a program that asks for the student name, student roll
number, marks in subjects (Science, English, IT, and Mathematics). The program
than calculates the percentage of the student and displays the result on screen
with the student name and the total marks he, she scored. Assume total marks to
be 400.
We will begin the pseudocode like this:
OUTPUT “Enter name of the student: ”
INPUT Student_name
OUTPUT “Enter student’s roll number: ”
INPUT Student_roll_number
OUTPUT “Enter marks in Science: ”
INPUT Marks_in _Science
OUTPUT “Enter marks in English: ”
INPUT Marks_in _English
OUTPUT “Enter marks in IT ”
INPUT Marks_in _IT
OUTPUT “Enter marks in Mathematics: ”
INPUT Marks_in _Mathematics
Total_marks 400
Obtained_marks Marks_in_Science + Marks_in_English + Marks_in_IT +
Marks_in_Mathematics
Percentage (Obtained_marks / Total_marks) * 100
OUTPUT Student_name, “scored”, Obtained_marks, “out of”, Total_marks
OUTPUT Percentage
Practice Questions
1. Write pseudocode that asks for your age and displays the total number of
days you have passed.
2. Write pseudocode that asks the user to enter distance covered by a car and
the time taken. The pseudocode should calculate the average speed and
display it.
3. Write pseudocode to display the following pattern:
@@@@@
@@@@
@@@
@@
@
4. Write pseudocode to calculate the number of seconds in a year.
5. Write pseudocode to merge two words on the display screen.
• IF…THEN statement
• IF…THEN…ELSE statement
• Nested IF structure
IF…THEN Statement
IF condition THEN statement(s)
ENDIF
Or
IF condition
THEN statement(s)
ENDIF
Or
IF condition
THEN
IF condition
THEN statement(s)
ENDIF
ENDIF
You can use any of the above syntax.
1. To check if number of students are competed.
IF Number_of_students < 40 THEN OUTPUT “Seats available.”
ENDIF
2. To check if name is a certain name or not.
IF Name <> “Martha”
THEN
OUTPUT “Identity Mismatch”
INPUT Name
ENDIF
In all the examples above, the statements with THEN are executed only if the
condition mentioned with IF statement turns out to be True.
Limitation: In case if the condition is False, we see no alternate action taking
place. The screen will be blank in above cases.
IF…THEN…ELSE Statement
The IF THEN ELSE statement is used when we to perform alternate actions
regardless of whether the IF statement evaluates to true or false. If the condition
specified with IF is true THEN statement is executed, if it is false, ELSE statement
is executed.
IF condition THEN statement(s) ELSE statement(s)
ENDIF
Or
IF condition
THEN statement(s)
ELSE statement(s)
ENDIF
Or
IF condition
THEN
IF condition
THEN statement(s)
ELSE statement(s)
ENDIF
ELSE
statement(s)
ENDIF
Depending upon the program, available space on page and time You can use any of
the above syntax.
Example
In a factory, employees have to work 50 hours a week to get a wage of 10,000$. The
overtime rate is 20$ per hour. Write a pseudocode code that asks the employee to
enter number of hours worked in a week and checks if the employee gets overtime
bonus or reduction in payment for working a smaller number of hours than 50. The
reduction is 150$ per hour.
Base_wage 30000
Over_time_rate 20
Penalty_rate 150
OUTPUT “Enter number of hours worked: ”
INPUT Hours_worked
IF Hours_worked >= 50
THEN
Over_time_bonus (Hours_worked - 50) * Over_time_rate
Total_wage Base_wage + Over_time_bonus
OUTPUT “Total wage is: ”, Total_wage
ELSE
Penalty (50 – Hours_worked) * Penalty_rate
Total_wage Base_wage – Penalty
OUTPUT “Total wage is: ”, Total_wage
ENDIF
Nested IF statement
A nested structure means a conditional statement within another conditional
statement. A nested IF statement can have the following structure:
IF condition
THEN
IF condition
THEN
IF condition
THEN statement(s)
ELSE statement(s)
ENDIF
ENDIF
ENDIF
Example
Write pseudocode to check if a year is a leap year or not.
OUTPUT “Enter a year: ”
INPUT Year
IF Year % 4 = 0
THEN
IF Year % 100 = 0
THEN
IF Year % 400 = 0
THEN OUTPUT “It is leap year.”
ELSE OUTPUT “It is not a leap year”
ENDIF
ELSE OUTPUT “It is a leap year”
ENDIF
ELSE OUTPUT “It is not a leap year.”
ENDIF
CASE…OF…OTHERWISE…ENDCASE Statement
This statement is used when we have multiple options to select from and one of the
options needs to be selected.
CASE variable OF
Value1: statement
Value2: statement
Value3: statement
.
.
.
ValueN: statement
OTHERWISE statement
ENDCASE
In the above syntax the CASE statement is used to check the variable value. The
variable holds a value specified in the program or entered by the user. The value of
that variable is matched with the values below (Value1, Value2, Value3 up to N
number of values). Each value has statement(s) mentioned with it. In case of
matching value, its statements are executed.
In case if no value matches with the variable’s value then the statements
mentioned with OTHERWISE are executed and at the end the case structure ends
with an ENDCASE statement.
Example
In a game in order to set reward scores for treasure chests.
OUTPUT “Enter which treasure chest to open (1, 2, 3, 4, 5, 6): ”
INPUT Chest_number
Choice Chest_number OF
1: Score Score + 50
2: Score Score + 34
3: Score Score + 72
4: Score Score + 152
5: Score Score + 10
6: Score Score + 28
OTHERWISE OUTPUT “You picked the wrong chest.”
ENDCASE
FOR…TO…NEXT statement
When it is known in advance that how many times, we need to repeat the
statements to get our final output, FOR…TO…NEXT statement fits the job.
FOR variable x TO y STEP z
Statement 1
Statement 2
Statement 3
…
…
Statement n
NEXT
In the above syntax a variable is used with FOR which is set to a starting limit x
(for example 1 or 0) and an ending limit y (for example 10, 1000, 237236178 etc.).
The loop will repeat the statements inside it based on the difference between y and
x. X and Y should be integers (whole numbers). The variable used with FOR is also
called the loop control variable.
The NEXT keyword is used to repeat the next cycle of the loop also known as an
iteration.
Example
FOR Count 1 TO 10
OUTPUT “This sentence will be repeated 10 times.”
NEXT
OUTPUT
OUTPUT
This sentence will be repeated 10 times.
1
This sentence will be repeated 10 times.
2
This sentence will be repeated 10 times.
3
This sentence will be repeated 10 times.
4
This sentence will be repeated 10 times.
5
This sentence will be repeated 10 times.
6
This sentence will be repeated 10 times.
7
This sentence will be repeated 10 times.
8
This sentence will be repeated 10 times.
9
This sentence will be repeated 10 times.
10
Example
To display the variable value as output.
FOR Count 1 TO 10
OUTPUT Count
NEXT
We can use the STEP command to set a step of z (gap). OUTPUT
Example 2
FOR EVEN 2 TO 20 STEP 2 4
OUTPUT EVEN 6
NEXT 8
Once the loop is completed, the statements after the loop are 10
executed.
12
The FOR…TO…NEXT is the easiest loop structure to set up. We
can also have a nested structure of FOR…TO…NEXT loop. In that 14
case the inner loop will run for its entire limit for a single value of
16
the outer loop.
18
FOR loop is also handy when assigning values to the index of an
array as we will see later. 20
REPEAT…UNTIL statement
FOR loop works best when you know how many times to repeat the statements, but
in case where it is not known how many times the statements will be executed until
the output is achieved, REPEAT…UNTIL or WHILE…DO…ENDWHILE are used.
REPEAT
Statement 1
Statement 2
Statement 3
…
…
Statement n
UNTIL criteria matches
Example
Suppose you want the user to guess the number you have set as the lucky number
between 0 and 100. In our case let’s assume that the lucky number is 95 and the
user needs to guess it. In such a case we are not sure that after how many tries the
user will guess the number? It can be after 10 tries, 20 tries or may be even the
first attempt.
In such a case we need to set a flag condition (a criteria) that if the number user
enters is matched with the set number than terminate the loop and move on to
other statements, otherwise keep on asking him for the guess.
The REPEAT…UNTIL statement can be used here to repeat the action of asking the
user to input the lucky number until the number is picked. The program will keep
on prompting the user to enter the number until the number entered matches the
set lucky number.
Lucky_number = 95
REPEAT
OUTPUT “Enter guess number: ”
INPUT Number
UNTIL Number = Lucky_number
Example
REPEAT
GAMEPLAY_ON
UNTIL Player_hp < 1
Statement(s) after the loop
Until the criteria is met, REPEAT will keep on repeating the statements mentioned
in its block. When the criteria is matched, the loop terminates and the statements
after the loop are executed.
WHILE…DO…ENDWHILE statement
In case of REPEAT…UNTIL loop, the statements in the loop are executed first and
then it is checked if the criteria is met or not. In WHILE…DO…ENDWHILE loop a
condition is first checked and as long as the condition is true, the statements
inside the loop will be executed.
WHILE condition is true
DO
Statement 1
Statement 2
Statement 3
…
…
Statement n
ENDWHILE
The statements inside the loop will be executed only if the condition after the
WHILE loop holds to be true. In case if it is false, the loop will not enter its block
and just get terminated.
Example
To check if a number is prime number.
OUTPUT “Enter number: ”
INPUT Num
N 2
Flag False //a bool type variable
WHILE N < Num
DO
IF Num % N = 0
THEN
Flag True
N Num //To stop the loop. We can also use “BREAK” keyword.
ELSE
N N + 1
ENDIF
ENDWHILE
IF Check = False
THEN OUTPUT “Number is Prime.”
ELSE OUTPUT “Number is not Prime.”
ENDIF
A0 A1 A2 A3 A4 A5 A6
𝐴(6) = 5 10 15 20 25 30 null
Notice that the array has 6 elements {5, 10, 15, 20, 25, 30} and each element has
the address A0, A1, A2, A3, A4, A5 assigned to it.
Note: There is a difference between a list and an array structure.
Two common types of array that are encountered are:
1-Dimensional array
2-Dimensional array
Method 2
Another way to populate an array is through loops, and the easiest to use with an
array is the FOR loop.
Example
Write pseudocode that finds the average of given list of marks of students:
Sum = 0
OUTPUT “Enter number of students: ”
INPUT Number_of_students
Student_marks[Number_of_students + 1] AS Integer
FOR Count 1 TO Number_of_students
OUTPUT “Enter marks of student number ”, Count
INPUT Student_marks[Count]
Sum Sum + Student_marks[Count]
NEXT
OUTPUT “Average marks are: ”, Sum / Number_of_stduents
Example
Write pseudocode that stores five names in an array and prints them in reverse
order:
Names[6] AS String
FOR Count 1 TO 5
OUTPUT “Enter name: ”
INPUT Names[Count]
NEXT
FOR Count 5 TO 1 STEP -1
OUTPUT Names[Count]
NEXT
Initializing a 2-Dimensional array
A 2-Dimensional array is same as a table with rows and columns. To initialize a 2-
Dimensional array we use the following syntax:
Array_name[number of rows] [number of columns] AS data type of array
Example
To define a matrix with 4 rows and 3 columns:
Game_grid[4][3] AS Integer
You can conceptualize the above array as follow
• Since we started our count from 0 therefore max row# is 3 and max column#
is 2.
• The above array can store 12 entries in it. To get how many entries the array
can store use the formula 𝑟𝑜𝑤𝑠 × 𝑐𝑜𝑙𝑢𝑚𝑛𝑠.
ENDPROCEDURE
OUTPUT “How many lines to print? ”
INPUT Num_lines
FOR Count 1 TO Num_lines Main Program
CALL Print_stars
NEXT
To use a function in our code we can use the following syntax:
FUNCTION Function_name(Parameters if any) RETURN Data_Type
Statements
…
ENDFUNCTION
The return data_type is the type of value that will be returned by the function to
the main program.
Function Definition
Example
FUNCTION Sum_of_series(Number : Integer) RETURN : Integer
Total (Number * (Number + 1)) / 2
RETURN Total
ENDFUNCTION
OUTPUT “Up to how many numbers you want the sum of starting from 1: ”
INPUT Num
Sum Sum_of_series(Num)
OUTPUT “Sum of the series is: ”, Sum
In the above example, we have a passed a parameter “Number” to the function
which basically means that when we are going to call the function we need to
supply the value of the parameter.
The value can be passed has a hardcoded input like Sum Sum_of_series(80) or
it can be a variable as in the above case. The important thing is that which ever
value you passed to the function must match the data type of the parameter as
stated in the function definition.
When the function is called in the program,
• We don’t have to use the CALL statement to call the function. We can just
write the name of the function to call it.
• We also need a variable matching the return type of the RETURN statement
in function definition. This variable will store the returned value of the
function.