[go: up one dir, main page]

0% found this document useful (0 votes)
15 views12 pages

Pseudocode

This is a little discussion about pseudocode in Computer Applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Pseudocode

This is a little discussion about pseudocode in Computer Applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Pseudocode

After this lesson you should be able to:


• Explain what is meant by pseudocode
• List the pseudocode statements for input, output, iteration, decision and
processing
• Use a pseudocode language to represent programs
• Use arithmetic, relational and logical operators in pseudocode
• Use sub processes/subroutines in pseudocode

What is pseudocode?

A Pseudocode is a written statement of an algorithm using a restricted and well-


defined vocabulary. Pseudocode is divided into groups:

• Input and output


• iteration
• decision
• and processing

Input and Output Statements

• 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 vs. 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

• These are the arithmetic operations like ADD, SUBTRACT, MULTIPLY,


DIVIDE.
• These also includes the SET operation where an initial value is given.

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

REPEAT The process in the repeat loop is executed until


<process> the value of count exceeds 12.
UNTIL count > 12

IF count <> total If the value of count is not the same as total the
THEN <process> process that follows THEN is executed.

Table 5.1 Examples of the Use of the Relational Operators

Arithmetic, logical and relational operators’ order of precedence:

• ( ) 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.

Max and Min Age Step-Form Algorithm

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

Max and Min Age Pseudocode


1. SET age = 0
SET max_age = 0
SET min_age = 1000
SET moreAges = “ “
2. DOWHILE (moreAges == Yes)
INPUT age
IF (age <min_age)
THEN min_age = age
ENDIF
IF (age >max_age)
THEN max_age = age
ENDIF
END DOWHILE
3. DISPLAY min_age
DISPLAY max_age
Example 5.2: Write a pseudocode that computes and displays the final price of the item.
The final price of the item is calculated as the price of the item plus the added tax. Note
that added tax is twelve percent of the price of the item.

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)

Example 5.8: Write a pseudocode that displays the total compensation of 10


employees. First ask the number of hours worked by the employee. Then ask if the
employee is a cashier or a seller. If cashier, the rate is 50.00/hr, if seller, the rate is
40.00/hr.

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

Example 5.10: Output all Even Numbers between 0 to 10.

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

Name: _____________________________ Date: ___________________________


Course & Year: _____________________________ Instructor: Juffil B. Papolonias MSc.
Subj. No. &Sec: _____________________________ Rating: ___________________________

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.

b. Write the pseudocode that will solve the problems stated.


c. Observe the proper format in writing a pseudocode.

III. Results and Discussion


a. Which is easier, step-form algorithm or pseudocode? Why?
b. What are the advantages and disadvantages of using pseudocode type of
algorithm?
IMPORTANT NOTES: The main differences between a step-form algorithm and
pseudocode lie in their structure, formality, and usage.

Key Differences:

Formality: Pseudocode is closer to real code, while a step-form algorithm is more


like an outline or plan of steps.

Structure: Step-form algorithms are usually presented as numbered steps, while


pseudocode uses indentation and more technical constructs like loops and
conditionals.

Purpose: Step-form algorithms are more educational and explanatory, while


pseudocode is often used to transition from conceptual thinking to coding.

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.

You might also like