You are on page 1of 15

Instant Pseudocode Revision 1

CLASS – 1 07/04/2023

Pseudocode:
1. It uses English key words to describe what an algorithm does.
2. The English key words used are similar to those used in a high-level programming language.
Basic Rules of Pseudocode:
1. All keywords and data types are written in capital letters such as DECLARE, CONSTANT,
STRING, REAL, INPUT, PRINT, IF, THEN, FOR, NEXT, WHILE, REPEAT, UNTIL, AND, OR etc.
2. All names given to data items and subroutines start with a capital letter.
3. All identifiers must be declared at the beginning of the code along with their data types.

Algorithm:
• An algorithm is produced when designing a solution to a problem.
• It is used to describe the solution as a sequence of steps/actions.

EXAM TIP:
Whenever a question statement in Paper 2 asks for a reason for why you would use a particular
type of conditional statement or a particular type of loop or any other reason asked for 1 mark
in Paper 2 usually has the following structured answer:
• It is used to simplify pseudocode OR make pseudocode more understandable.
• It is used to simplify programming OR make the code easier etc.

Assignments:
• The assignment operator is ‘’ but we will use ‘=’ throughout the practice and classes.
• The variables must be assigned values of the same data type as the variable itself.
Format of Assignments:
<identifier> = <value>
Example of Assignments:
1. Cost = 10.6
2. Counter = Counter + 1
3. Total = Num1 + Num2
4. Payment = NumberOfHours * HourlyRate
5. Tax = Price * 0.12
6. Gender = “M”
7. Name = “Umaid”
8. Grade = “O3”
9. Chosen = False
Instant Pseudocode Revision 2

Variable Declarations:
NOTE:
Format of Variable Declarations:
The data types must be capitalized during
DECLARE <identifier> : <data type> the declaration. The following keywords
Example of Variable Declarations: are used to declare basic data types:

1. DECLARE Counter : INTEGER INTEGER


2. DECLARE Name : STRING REAL
3. DECLARE Payment : REAL CHAR
4. DECLARE Choice : BOOLEAN STRING
5. DECLARE Initial : CHAR BOOLEAN

Constant Declarations:
• Only literals can be used as the value of constant.
• A variable, another constant or an expression must never be used.
Format of Constant Declarations:
CONSTANT <identifier> = <value>
Example of Constant Declarations:
1. CONSTANT HourlyRate = 6.50
2. CONSTANT Name = “Haseeb”
3. CONSTANT Number = 999
4. CONSTANT Flag = TRUE

Identifiers:
These are the names given to variables, constants, arrays, procedures and functions in mixed case
e.g. FirstName.
Constants:
• The value that cannot be changed accidentally during the execution of the program.
• The value only needs to be changed once if circumstances change or during the initialization
process.
Variables:
• It stores a value that can change during the execution of the program.
• It can use a variable without knowing its value.
Arrays:
• It is a list of items of the same data type stored under a single name.
• It is used to reduce the number of variables.
• Any item can be found using an index number to show its place in the list.
Instant Pseudocode Revision 3

Rules for Identifiers:


1) They should have meaningful names (they describe the variable, constant, or array to which
they refer).
2) The keywords should never be used as identifier names.
3) They can only contain letters (A–Z, a–z) and digits (0-9).
4) They must start with a capital letter and not a digit.
5) They should never start with a digit but they can end with a digit.
6) The accented letters (á, é, î, ô, Ã, Ñ, ÿ, Ä etc.) and other characters (@, $, #, ^, %, * etc.),
including the underscore, should not be used.
7) The single letters may be used where these are conventional (such as i and j when dealing
with array indices, or X and Y when dealing with coordinates).
The identifiers should be considered case insensitive, for example, Countdown and CountDown
should not be used as separate variables.

Data Types:
1. Integer: It is a whole number that can be positive, negative or zero.
e.g. –10, 999, 12345, 2001, 3, –8765
2. Real: It is a number with a fractional part that can be positive or negative.
e.g. 2.0, –3.142, 999.87
3. String: It is a sequence of characters that may contain letters and/or numbers and/or special
characters.
e.g. Haseeb, lahore123, def@773, #123?Y, IP123.112
4. Char: It is a single character that may contain a single number, symbol or letter.
e.g. A, #, 2, @, x, T
5. Boolean: It is a data type with two possible values, such as TRUE and FALSE.
e.g. TRUE/FALSE, YES/NO, 0/1

NOTE:
The integers are written as normal without any quotations, e.g. 5, –3
The real is written with at least one digit on either side of decimal point, e.g. 4.7, 0.3, 0.0, –4.0
The char is a single character written with single quotations, e.g. ‘x’, ‘c’, ‘@’
The string is a sequence of characters written with double quotations, e.g. “This is a string”
The string may contain no characters (the empty string), e.g. “”
The boolean is written in capital letters without any quotations e.g. TRUE, FALSE
Instant Pseudocode Revision 4

Practice Questions of Data Types


Question 1:

Question 2:

String

String/Char

String

Real

Boolean
Instant Pseudocode Revision 5

Question 3:

Integer

It is a whole number that can be positive, negative or zero.

String

It is a sequence of characters that may contain letters and/or numbers

and/or special characters.

Real

It is a number with a fractional part that can be positive or negative.


Instant Pseudocode Revision 6

Conditional Statements:
Examples of Conditional Statements:
1. IF (...THEN...ELSE...ENDIF)
2. CASE (...OF...OTHERWISE...ENDCASE)
Purpose of Conditional Statements:
• They are used to allow different routes through a program dependent on meeting certain
criteria.

IF Statement:
A conditional statement with different outcomes for true and false.
• It is used for checking a condition that may be complex.
• It is used for checking a condition that uses relational operators.
• It is used for checking for a range of values.
• It is used for checking for only 2 options.
Format of IF Statements:
• The IF Statements may or may not have an ELSE clause.
IF Statements without an ELSE clause are written as follows:

IF <condition>
THEN
<statements>
ENDIF

IF Statements with an ELSE clause are written as follows:

IF <condition>
THEN
<statements>
ELSE
<statements>
ENDIF
Instant Pseudocode Revision 7

Example Code of IF Statement:

IF X > 0 AND X < 10


THEN
PRINT “In Range”
ELSE
PRINT “Out of Range”
ENDIF

Example Code of Nested IF Statement:

IF ChallengerScore > ChampionScore


THEN
IF ChallengerScore > HighestScore
THEN
PRINT ChallengerName, “ is champion”
ELSE
PRINT Player1Name, “ is the new champion”
END IF
ELSE
PRINT ChampionName, “ is still the champion”
IF ChampionScore > HighestScore
THEN
PRINT ChampionName, “ is also the highest scorer”
ENDIF
END IF
Instant Pseudocode Revision 8

CASE Statement:
A conditional statement to deal with many possible outcomes.
• It is used for checking for discrete values.
• It is used for checking more than 2 of values.
Format of CASE Statements:
• The CASE Statements allow one out of several branches of code to be executed, depending
on the value of a variable.
CASE Statements are written as follows:

CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
ENDCASE

An OTHERWISE clause can be the last case:

CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
OTHERWISE <statement>
ENDCASE
Instant Pseudocode Revision 9

Example Code of CASE Statement:

CASE OF X
1 : PRINT “Option 1”
2 : PRINT “Option 2”
3 : PRINT “Option 3”
OTHERWISE PRINT “Incorrect Choice”
ENDCASE

Example Code of CASE Statement:

INPUT Move
CASE OF Move
‘W’ : Position = Position – 10
‘E’ : Position = Position + 10
‘A’ : Position = Position – 1
‘D’ : Position = Position + 1
OTHERWISE PRINT “Wrong Movement”
ENDCASE
Instant Pseudocode Revision 10

Basic Pseudocode Examples:


Example 1: calculating and displaying sum of two numbers

Pseudocode Dry Run

DECLARE Num1, Num2, Total : INTEGER


Total = 0
PRINT “Please enter first number” Please enter first number
INPUT Num1 3
PRINT “Please enter second number” Please enter second number
INPUT Num2 4
Total = Num1 + Num2 Total = 3 + 4
PRINT Total 7

Example 2: calculating and displaying average of three numbers

Pseudocode Dry Run

DECLARE Num1, Num2, Num3, Total : INTEGER


Total = 0
DECLARE Avg : REAL
PRINT “Please enter three numbers” Please enter three numbers
INPUT Num1, Num2, Num3 4, 3, 5
Total = Num1 + Num2 + Num3 Total = 4 + 3 + 5
Avg = Total/3 Avg = 12/3
PRINT Avg 4
Instant Pseudocode Revision 11

CLASS – 2 08/04/2023

Pseudocode Examples for Conditional Statements:


Example 1 – calculating and displaying larger of two numbers

IF Statement Dry Run

DECLARE Num1, Num2, Larger : INTEGER


Larger = 0
PRINT “Please enter two numbers” Please enter two numbers
INPUT Num1, Num2 7, 12
IF Num1 > Num2 THEN 7 > 12 (FALSE)
Larger = Num1
ELSE 12 > 7 (TRUE)
Larger = Num2 Larger = 12
ENDIF
PRINT Larger 12

CASE Statement Dry Run

DECLARE Num1, Num2, Larger : INTEGER


Larger = 0
PRINT “Please enter two numbers” Please enter two numbers
INPUT Num1, Num2 7, 12
CASE OF Larger
Num1 : Num1 > Num2 Num1 : 7 > 12 (FALSE)
Num2 : Num2 > Num1 Num2 : 12 > 7 (TRUE)
OTHERWISE PRINT “Invalid number”
ENDCASE
PRINT Larger 12
Instant Pseudocode Revision 12

Example 2 – calculating and displaying larger of three numbers

IF Statement Dry Run

DECLARE Num1, Num2, Num3, Larger : INTEGER


Larger = 0
PRINT “Please enter three numbers” Please enter three numbers
INPUT Num1, Num2, Num3 2, 8, 4
IF Num1 > Num2 AND Num1 > Num3 THEN 2 > 8 (FALSE) AND 2 > 4 (FALSE)
Larger = Num1
ELSEIF Num2 > Num1 AND Num2 > Num3 THEN 8 > 2 (TRUE) AND 8 > 4 (TRUE)
Larger = Num2 Larger = 8
ELSE
Larger = Num3
ENDIF
PRINT Larger 8

CASE Statement Dry Run

DECLARE Num1, Num2, Num3, Larger : INTEGER


Larger = 0
PRINT “Please enter three numbers” Please enter three numbers
INPUT Num1, Num2, Num3 2, 8, 4
CASE OF Larger
Num1 : Num1 > Num2 AND Num1 > Num3 Num1 : 2 > 8 (FALSE) AND 2 > 4 (FALSE)
Num2 : Num2 > Num1 AND Num2 > Num3 Num2 : 8 > 2 (TRUE) AND 8 > 4 (TRUE)
Num3 : Num3 > Num1 AND Num3 > Num2
ENDCASE
PRINT Larger 8
Instant Pseudocode Revision 13

Example 3 – calculating and displaying grades

IF Statement

DECLARE Marks : INTEGER


DECLARE Grade : STRING
PRINT “Please enter your marks”
INPUT Marks
IF Marks >= 90 THEN
Grade = “A*”
ELSEIF Marks >= 80 THEN
Grade = “A”
ELSEIF Marks >= 70 THEN
Grade = “B”
ELSE Marks < 70 THEN
Grade = “U”
ENDIF
PRINT Grade

CASE Statement

DECLARE Marks : INTEGER


DECLARE Grade : STRING
PRINT “Please enter your marks”
INPUT Marks
CASE OF Grades
“A*” : Marks >= 90
“A” : Marks >= 80
“B” : Marks >= 70
“U” : Marks <70
ENDCASE
PRINT Grade
Instant Pseudocode Revision 14

Example 4 – displaying the day of the week based on a number

IF Statement

DECLARE Num : INTEGER


DECLARE Day : STRING
PRINT “Please enter a number between 1 to 7”
INPUT Num
IF Num = 1 THEN
PRINT “Monday”
ELSEIF Num = 2 THEN
PRINT “Tuesday”
ELSEIF Num = 3 THEN
PRINT “Wednesday”
ELSEIF Num = 4 THEN
PRINT “Thursday”
ELSE PRINT “Friday, Saturday or Sunday”
ENDIF

CASE Statement

DECLARE Num : INTEGER


DECLARE Day : STRING
PRINT “Please enter a number between 1 to 7”
INPUT Num
CASE OF Num
1 : Day = “Monday”
2 : Day = “Tuesday”
3 : Day = “Wednesday”
4 : Day = “Thursday”
5 : Day = “Friday”
6 : Day = “Saturday”
7 : Day = “Sunday”
ENDCASE
PRINT Day
Instant Pseudocode Revision 15

Practice Questions of Conditional Statements


Question 1: (3 marks)

PRINT “Enter a number between 0 and 100”


1 mark
INPUT Number

IF Number < 0 OR Number > 100 1 mark

THEN

PRINT “The number is outside the specified range” 1 mark

ENDIF

EXAM TIP:
This question only carries 3 marks. We will not declare or initialize any variable because it would
take a lot of time and there are no additional marks for that. We will only do those steps when
the variables/constants are more in quantity and the question is asked for 5 or more marks in
the examination.
The following is examiners marking criteria for this specific question:
• Prompt for input and input of number (1 mark)
• Checking the input number is between 0 and 100 inclusive (1 mark)
• Output of a correct error message (1 mark)

You might also like