Computer Science (9618)
Pseudocode Guide
Commenting:
//Program made by Ali.
Variable:
The value of variable can change throughout the execution of a
program.
Variable Data Types:
INTEGER: 23
REAL: 23.67
CHAR: “a”
STRING: “My name is Ali.”
BOOLEAN: TRUE
DATE: 22/12/2022
Variable Data Type Declaration:
Variable declaration is done at the start of a program.
DECLARE num : INTEGER
DECLARE name : STRING
DECLARE answer : BOOLEAN
Constant:
Its value does not change throughout the execution of a
program.
Constant Declaration:
CONSTANT pi = 3.14
CONSTANT price = 25
OUTPUT Statement:
OUTPUT “My name is Ali.”
price = 25
OUTPUT “The price is:”, price
INPUT Statement:
INPUT num
INPUT name
Assignment Statement:
It is a statement where you assign a value to a variable. It can
be identified by an equals to “=” sign.
price = 25
sum = num1 + num2
area = 3.14 * radius * radius
Task for the Student:
Q. Write a program to input the radius of circle from the user
and find its area. Also declare the variable and constants in the
program.
Q. Write pseudocode for a program that takes four numbers as
an input from the user. Find the product of the first two
numbers and find the difference of the third and fourth
number.
Operators:
Assignment Operator:
“=” is known as assignment operator.
Mathematical Operators:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponent: ^
Group: ()
Comparison Operators:
Less than: <
Less than and equals to: <=
Greater than: >
Greater than and equals to: >=
Equals to: =
Not equals to: <>
Logical Operators / Boolean Operators:
AND
OR
NOT
Conditional Statements:
IF THEN ELSE Statement
CASE Statement
IF THEN ELSE Statement:
Basic Structure:
IF condition1
THEN execution1
ELSE execution2
ENDIF
NESTED IF Basic Structure:
IF condition1
THEN execution1
ELSE IF condition2
THEN execution2
ELSE IF condition3
THEN execution3
ELSE execution4
ENDIF
ENDIF
ENDIF
Example:
Q. Write pseudocode for a program that inputs a number from
the user and print whether the number is positive, negative or
a zero.
Solution:
OUTPUT “Enter a number:”
INPUT num
IF num > 0
THEN OUTPUT “Positive”
ELSE IF num < 0
THEN OUTPUT “Negative”
ELSE OUTPUT “Zero”
ENDIF
ENDIF
Task for Student:
Q. Write pseudocode for a program that inputs the marks of
the student and prints whether the student has passed or failed
in an exam. The passing marks are 50.
Q. Write pseudocode for a program that inputs three numbers
from the user and print the highest and the smallest number.
Q. Write pseudocode for a program that inputs two numbers
and a choice from the user. The program should be able to add,
subtract, multiply and divide the two numbers according to the
choice of the user.
Case Statement:
Basic Structure:
CASE variable OF
condition1 : execution1
condition2 : execution2
condition3 : execution3
OTHERWISE execution4
ENDCASE
Example:
Q. Write pseudocode for a program that inputs a number from
the user and print whether the number is positive, negative or
a zero.
Solution:
OUTPUT “Enter a number:”
INPUT num
CASE num OF
> 0 : OUTPUT “Positive”
< 0 : OUTPUT “Negative”
OTHERWISE OUTPUT “Zero”
ENDCASE
Task for Student:
Q. Write pseudocode for a program that inputs the percentage
of the student and then print his grade according to the
following criteria:
Percentage greater than 90 is A Grade.
Percentage between 81 and 90 is B Grade.
Percentage between 71 and 80 is C Grade.
Percentage between 61 and 70 is D Grade.
Percentage between 51 and 60 is E Grade.
Otherwise the Grade is U.
Loops:
Loops are used in programming to repeat a particular part of the
code multiple times.
There are three types of loops in pseudocode:
FOR Loop: It is used when the number of repetitions are known.
REPEAT UNTIL Loop: It is used when the number of repetitions
are unknown. It is called a post-condition loop. The body of the
loop will at least execute once since the condition is checked at
the end.
WHILE Loop: It is used when the number of repetitions are
unknown. It is called a pre-condition loop. The body of the loop
may not execute once since the condition is checked at the start
of the loop.
FOR Loop:
Basic Structure:
FOR variable = starting_value TO ending_value
Body of the loop (Any code that needs repetition)
NEXT variable
Example:
FOR a = 1 TO 10
OUTPUT “Ali”
NEXT a
Task for Student:
Q. Write pseudocode to input the name of the user and print it
ten time.
Q. Write pseudocode to input the names of ten students and
print each of them one time.
Q. Write a program to print number from 10 to 50.
Q. Write a program to input the starting number and an ending
number from the user. The program should then print the all
the numbers from the starting to the ending value inclusive.
REPEAT UNTIL Loop:
Basic Structure:
REPEAT
Body of the loop
UNTIL condition(when condition is true, loop will stop.)
Example:
Q. Write a program to input numbers from the user and print
them. The program to stop when the user enter 0 as an input.
Solution:
REPEAT
OUTPUT “Enter a number or 0 to stop:”
INPUT num
OUTPUT “You entered:”, num
UNTIL num = 0
Task for Student:
Q. Write pseudocode that inputs different names. The program
should stop when a user enters a particular text. Also declare
the variables.
Example:
Q. Write pseudocode to input 25 numbers from the user and
print them using REPEAT UNTIL Loop.
Solution:
a=0
REPEAT
OUTPUT “Enter a number:”
INPUT num
OUTPUT “You entered:”, num
a=a+1
UNTIL a = 25
Task for Student:
Q. Write a program using REPEAT UNTIL Loop to input the
starting number and an ending number from the user. The
program should then print the all the numbers from the
starting to the ending value inclusive.