Pseudocode
Pseudocode
What is pseudocode?
• INPUT, READ
Used to get values from a data source, a keyboard for instance
• DISPLAY
Used to output values to a data sink, a screen or printer for instance
Example
1) INPUT number
2) INPUT age
3) READ given
4) READ page
5) DISPLAY new_value
6) DISPLAY output
Iterative Statements
• The REPEAT loop executes the process until the proposition becomes true.
REPEAT
<process>
UNTIL <proposition>
• The WHILE loop executes the process while the proposition is true.
DOWHILE <proposition>
<process>
END DOWHILE
• Repeat
Put water in kettle
Until kettle is full
• The process is Put water in kettle, the proposition is kettle is full.
• The repeat loop does some processing before testing the state of the
proposition, what happens though if in the example above the kettle is already
full? If the kettle was already full at the start of the repeat loop then putting
more water in will lead to an overflow.
• In this case the While loop is more appropriate:
• While kettle is not full put water in kettle
Example
1) SET count to 0
REPEAT
DISPLAY count
ADD 1 to count
UNTIL count > 10
2) SET count to 0
DOWHILE count < 10
DISPLAY count
count = count + 1
END DOWHILE
Note that the statements inside the loops are indented to aid the readability of the
pseudocode.
Decisive Statements
• IF <proposition>
THEN <process>
ENDIF
• IF <proposition>
THEN <process1>
ELSE <process2>
ENDIF
Example
1) IF count > 10
THEN DISPLAY count
ENDIF
2) IF count > 10
THEN DISPLAY 'count > 10'
ADD 4 to sum
ELSE DISPLAY 'count <= 10'
ADD 3 to sum
ENDIF
Processes
Example
1) ADD 3 TO count
2) SUBTRACT 5 FROM count
3) SET count TO 12
Arithmetic operators
Although the processing group is very useful, most program designers tend to prefer
to use operators like:
• + add
• - subtract
• * multiply
• / divide
• = assign
These are more intuitive since we use them in arithmetical expressions and they are
easier to write, for instance:
1) count = count + 22
2) m = count / 12
3) sum = sum - count
4) x = count * sum
The assignment operator “=” means set the results of the operation on the right-hand
side to the variable on the left-hand side.
Relational operators
These are used to evaluate the relationship between variables. These are:
• == equal to (a pair of equals signs)
• <= less than or equal to
• >= greater than or equal to
• <> not equal to
IF count == 32 This decision states that if the variable count
THEN <process> contains 32 execute the process following THEN.
DOWHILE count <= 50 The process in the while loop is executed while
<process> the value of count is 50 or less.
END DOWHILE
IF count <> total If the value of count is not the same as total the
THEN <process> process that follows THEN is executed.
• ( ) parentheses
• NOT
• * / AND
• + - OR
• == > < <> >= <=
The precedence is from top to bottom and on each line from left to right. Evaluate
whatever is in parentheses first, then NOT followed by *, /, AND then +, -, OR then last
the relational operators.
Don't confuse == (equal) with = (assignment). The first determines if a variable is the
same as a value or another variable, but the second (assignment) makes a variable
become some value. Remember == is two equals signs.
A statement like count <= 50 is a proposition, you might remember from an earlier
lesson that a proposition is a statement that has a truth value, it can be TRUE or
FALSE. If count is less than or equal to 50 then the proposition count <= 50 is true.
For example:
1) count = 51
2) IF count <= 50 THEN DISPLAY 'Count is less than 51' ENDIF
3) IF count > 50 THEN DISPLAY 'Count is greater than 50' ENDIF
Example 5.1: Convert the Max and Min Age Step-Form Algorithm into Pseudocode.
1. age = 0
2. max_age = 0
3. min_age = 1000
4. Enter your age
5. IF age <min_age THEN min_age = age
IF age >max_age then max_age = age
6. INPUT Any more ages (Yes/No)
7. IF Yes, repeat step 4.
IF No, display min_age and max_age
1. SETprice_of_item=0
SET tax = 0.0
SET taxRate = 0.12
SET price = 0.0
2. INPUT price_of_item
3. tax = price_of_item * tax_rate
4. price= price_of_item + tax
5. DISPLAY price
Example 5.3: Create a pseudocode that displays the least and greatest number from
the given 10 numbers ranging from 0 to 1000.
1. SET least=1000
SET greatest=0
SET number=0
SET counter=0
2. REPEAT
INPUT number
IF(number>greatest)
THEN greatest=number
END IF
IF(number<least)
THEN least=number
END IF
counter=counter+1
UNTIL(counter==10)
3. DISPLAY least
DISPLAY greatest
Example 5.4: Create a pseudocode that output odd numbers between numA and numB.
1. SET numA=0
SET numB=0
2. INPUT numA
INPUT numB
3. REPEAT
numA=numA+1
IF(numA%2==1)
THEN DISPLAY numA
END IF
UNTIL (numA==(numb-1))
Example 5.5: Write a pseudocode that displays the product of the base raised to the
exponent.
1. SET base=0
SET exponent=0
SET product=1
2. INPUT base
INPUT exponent
3. REPEAT
product=product*base
exponent=exponent-1
UNTIL (exponent==0)
4. DISPLAY product
Example 5.6: Write the pseudocode that displays the factorial of a number.
1. SET num=0
SET factorial=1
2. INPUT num
3. REPEAT
factorial=factorial*num
num=num-1
UNTIL(num==0)
4. DISPAY factorial
Example 5.7: Write a pseudocode that counts 1 to 10.
1. SET counter=0
2. REPEAT
counter=counter+1
DISPLAY counter
UNTIL (counter==10)
1. SET hrs=0
SET employee= “ ”
SET counter=0
SET rC=50.00
SET rS=40.00
SET wage=0
SET totalWage=0
2. DOWHILE(counter<10)
INPUT hrs
INPUT employee
IF(employee==”cashier”)
THEN wage=hrs*rC
ELSE wage=hrs*rS
END IF
totalWage=totalWage+wage
END DOWHILE
3. DISPLAY totalWage
Example 5.9: Write the pseudocode that displays the average weight of 100 persons.
1. SET weight=0
SET total=0
SET counter=0
SET average=0
2. REPEAT
INPUT weight
total=total+weight
counter=counter+1
UNTIL(counter==100)
3. average=total/100
4. DISPLAY average
1. SET num = 0
SET limit = 10
2. DOWHILE (num < limit)
num = num + 1
IF (num % 2 == 0)
THEN DISPLAY num
END IF
END DOWHILE
CCC100 – FUNDAMENTALS OF COMPUTING
LABORATORY 4
Pseudocode
I. Objectives
a. To know how to write a pseudocode type of algorithm.
b. To apply the correct format in writing a pseudocode.
II. Procedure
a. Consider the problems below.
i. Ask the user to input two resistor values. Then ask the user if the
circuit is in series or in parallel configuration. If in series, input 'S'
and if in parallel, input 'P'. Then display the total resistance value of
the two resistors used.
ii. A car travels from A to B, then from B to C then back from C to A.
If it took the car to spend 3 liters from A to B, 5 liters from B to C
and 10 liters from C to A, how many liters will the car need for the
whole month if it needs to cover 20 laps per day?
iii. Input 100 numbers from the user. From the 100 numbers
determine and display the sum of all even numbers and the sum of
all odd numbers. Then display the average of the two sums.
Key Differences:
Both are important in problem-solving, but pseudocode is usually the next step
after a step-form algorithm when translating a plan into something that resembles
code.