Lesson 1- Introduction
To Pseudocode
Definition of Pseudocode:-
You can write algorithms in plain English before you decide which programming
language you want to use. Writing algorithms this way is called pseudocode.
Let’s take an example
Write pseudocode to find the larger number between
two numbers .
Let’s start
PSEUDOCODE
Rules for writing pseudocode:
• Variables
• Identifier ← Exp
• a←3
• b←a+1
• c ← 'Hello’
A variable is declared the first time a value is assigned.
It assumes the data type of the value it is given.
Variables declared inside a function or procedure are
local to that subroutine. Variables in the main program
can be made global with the keyword global.
• Constant Assignment
• CONSTANT IDENTIFIER ← Exp
• CONSTANT PI ← 3.141
• CONSTANT CLASS_SIZE ← 23
# Names of constants will always be
# written in capitals
• Outputting to Screen
OUTPUT StringExp, … StringExp
OUTPUT a
OUTPUT a, g
# The output statement can be followed by
multiple StringExp separated by commas
• Input
User input
USERINPUT
a ← USERINPUT
• Logical Operators
• Logical AND
BoolExp AND BoolExp
(3 = 3) AND (3 ≤ 4)
• Logical OR
BoolExp OR BoolExp
(x < 1) OR (x > 9)
• Logical NOT
NOT BoolExp
NOT (a < b)
• Relational Operators
• Less than
Exp < Exp
4<6
'A' < 'B’
'adam' < 'adele’
• Greater than
Exp > Exp
4.1 > 4.0
• Equal to
Exp = Exp
3 =3
• Relational Operators
• Not equal to
Exp ≠ Exp
qty ≠ 7
• Less than or equal to
Exp ≤ Exp
3≤4
4≤4
• Greater than or equal to
Exp ≥ Exp
4≥3
4.5 ≥ 4.5
• Arithematic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
MOD modulus
• Indefinite (condition controlled) iteration
• REPEAT-UNTIL
(repeat the statements until the Boolean
expression is True) REPEAT
# statements here
UNTIL BoolExp
a←1
REPEAT
OUTPUT a
a←a+1
UNTIL a = 4
# will output 1, 2, 3
• Indefinite (condition controlled) iteration
• WHILE-ENDWHILE
(while the Boolean expression is True, repeat the
statements) WHILE BoolExp
# statements here
ENDWHILE
a←1
WHILE a < 4
OUTPUT a
a←a+1
ENDWHILE
# will output 1, 2, endif
• Definite (condition controlled) iteration
• FOR-TO-[STEP]- ENDFOR
(If STEP IntExp is missing it is considered to be 1.)
FOR Identifier ← IntExp TO IntExp [STEP IntExp]
# statements here
ENDFOR
# If STEP IntExp is omitted the step value is 1.
FOR a ← 1 TO 3
OUTPUT a
ENDFOR
# will output 1, 2, 3
FOR a ← 1 TO 5 STEP 2
OUTPUT a
ENDFOR
• Definite (condition controlled) iteration
• FOR-IN-ENDFOR
(repeat the statements the number of times that
there are characters in a string)
FOR Identifier IN StringExp
# statements here
ENDFOR
length ← 0
FOR char IN message
length ← length + 1
ENDFOR
# will calculate the # number of characters # in
message
• Definite (condition controlled) iteration
• IF-THEN-ENDIF
(execute the statements only if the Boolean
expression is True)
IF BoolExp
THEN
# statements here
ENDIF
a←1
IF (a MOD 2) = 0
THEN
OUTPUT 'even’
ENDIF
• Definite (condition controlled) iteration
• IF-THEN-ELSE-ENDIF
(execute the statements following the THEN if the
Boolean expression is True, otherwise execute the
statements following the ELSE)
IF BoolExp THEN
# statements here
ELSE
# statements here
ENDIF
a←1
IF (a MOD 2) = 0
THEN
OUTPUT 'even’
ELSE
OUTPUT 'odd’
Lets take another example
• Calculate the average of three numbers
Pseudocode to find average
a←0
b← 0
c←0
avg ← 0
avg ← (a+b+c)/3
OUTPUT avg
Lets take one more example
• Lets find out the given number is even number or odd
number
Thank You