0% found this document useful (0 votes)
4 views18 pages

Pseudocode Notes

The document provides definitions and examples of flowcharts, pseudocode, and various types of loops and selection statements used in programming. It includes detailed pseudocode examples for tasks such as calculating grades, bonuses, and electricity bills, as well as error corrections for common coding mistakes. Additionally, it covers the use of FOR loops and REPEAT...UNTIL loops, along with practical exercises for generating specific outputs.

Uploaded by

sami.4106
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Pseudocode Notes

The document provides definitions and examples of flowcharts, pseudocode, and various types of loops and selection statements used in programming. It includes detailed pseudocode examples for tasks such as calculating grades, bonuses, and electricity bills, as well as error corrections for common coding mistakes. Additionally, it covers the use of FOR loops and REPEAT...UNTIL loops, along with practical exercises for generating specific outputs.

Uploaded by

sami.4106
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

REVISION

Flowchart

Definition:
A flowchart is a diagrammatic (visual) representation of an algorithm using symbols.
Pseudocode

Definition:
Pseudocode is a way of writing an algorithm using simple English-like statements that resemble
programming code.

Key Features:

 Written in text form

 Easy to write and modify

 Uses keywords like: INPUT, OUTPUT, IF, FOR

 Does not follow strict syntax rules

Example:

INPUT number
IF number MOD 2 = 1 THEN
OUTPUT "Odd"
ELSE
OUTPUT "Even"
ENDIF

LOOPS

FOR…TO…NEXT (Count-Controlled Loop)

Definition:

A loop that repeats a fixed number of times.

Key Points:

 Used when the number of iterations is known


 Automatically increases/decreases the counter
 Has a clear start and end value

REPEAT…UNTIL (Post-Test Loop)

Definition:
A loop that always executes at least once and repeats until the condition becomes true.

Key Points:
 Condition checked after execution

 Runs at least once no matter what

WHILE…DO…ENDWHILE (Condition-Controlled Loop)

Definition:
A loop that repeats while a condition is true.

 Condition is checked before the loop runs

Types of Selection(Condition) Statements


IF…THEN

Definition:
Executes statements only when the condition is true.

Example:

IF marks >= 50 THEN


OUTPUT "Pass"
ENDIF

IF…THEN…ELSE

Definition:
Chooses between two alternatives (true or false).

Example:

IF age >= 18 THEN


OUTPUT "Adult"
ELSE
OUTPUT "Minor"
ENDIF
IF…THEN…ELSE ..IF

Definition:
Used when there are multiple conditions.

Example:

IF marks >= 80 THEN


OUTPUT "A"
ELSE IF marks >= 60 THEN
OUTPUT "B"
ELSE
OUTPUT "C"
ENDIF

CASE…OF…OTHERWISE…ENDCASE (Multiple Selection)

Definition:
A selection statement used when there are many possible outcomes.

Key Points:

 Cleaner than multiple IF statements

 Used for menu-based programs

 OTHERWISE handles unexpected values


Example:

CASE OF grade
"A": OUTPUT "Excellent"
"B": OUTPUT "Good"
"C": OUTPUT "Fair"
"D": OUTPUT "Needs Effort"
OTHERWISE: OUTPUT "Try Again"
ENDCASE

EXAMPLE QUESTIONS

Pseudo code to ask user marks and then decide if he is pass or fail.
DECLARE Marks : REAL
INPUT Marks
IF Marks < 50
THEN
OUTPUT "Fail"
ELSE
OUTPUT "Pass"
ENDIF
Pseudo code to ask user age and then decide if he is eligible to vote or not

HW

Topic: Pseudocode (IF…THEN…ELSE…IF)


Pseudocode to enter asks user marks and then display:
Marks >= 80  A Grade
Marks >= 60  B Grade
Marks >= 40  C Grade
Otherwise Fail
SOLUTION
DECLARE Marks : REAL
OUTPUT “Please enter your marks”
INPUT Marks
IF Marks>=80
THEN OUTPUT “You got A Grade”
ELSE IF Marks >=60
THEN OUTPUT “You got B Grade”
ELSE IF Marks>=40
THEN OUTPUT “You got C Grade”
ELSE OUTPUT “Fail”
END IF
Question #2 Input Salary and calculate bonus:
Salary >= 10,000 bonus is 20% bonus  0.2 * Salary
Salary >=5000 bonus is 10% bonus  0.1 * Salary
Otherwise bonus is 5% bonus  0.05 * Salary
Electricity Bill Calculation
Electricity charges are:
 First 100 units → $0.50 per unit
 Next 100 units (101–200) → $0.75 per unit
 Above 200 units → $1.00 per unit
Write pseudocode to input units used and calculate the total bill.
Pseudocode
DECLARE Units: REAL
DECLARE Bill: REAL
OUTPUT "Enter number of units used"
INPUT Units
IF Units <= 100
THEN Bill ← Units * 0.50
ELSE IF Units <= 200
THEN Bill ← Units* 0.75
ELSE Bill ← Units * 1.00
ENDIF
OUTPUT "Total bill is ", Bill

Date: 5th March,2026 Topic: REPEAT UNTIL LOOP


LOOP: A loop is a statement or group of statements that is executed
repeatedly until a specified condition is met or for a fixed number of
iterations.
REPEAT…UNTIL Loop
A repeat until loop repeats a set of instructions until a specific condition is
met.
REPEAT
OUTPUT “Please enter a number”
INPUT num
UNTIL num>10
EXAMPLES OF REPEAT…UNTIL
1) Keep Asking for a Password
DECLARE password : STRING
REPEAT
OUTPUT "Enter password:"
INPUT password
UNTIL password = "abc123"
OUTPUT "Access Granted"
2) Input a Number Until It Is Positive
DECLARE number : INTEGER
REPEAT
OUTPUT "Enter a positive number:"
INPUT number
UNTIL number > 0
OUTPUT "Thank you! You Have entered a negative number, please try
again"

3) Menu Choice (Valid Input Only)

DECLARE choice : INTEGER

REPEAT
OUTPUT "Enter 1 for Yes"
OUTPUT "Enter 2 for No"
INPUT choice
UNTIL choice = 1 OR choice = 2

OUTPUT "Valid choice entered"


Write pseudocode to:
 Input numbers from the user
 Keep adding them to a total Stop when the user enters -1
 Output the total
DECLARE number, total : INTEGER
total ← 0
REPEAT
OUTPUT "Enter a number (-1 to stop):"
INPUT number
total ← total + number
UNTIL number = -1

OUTPUT "Total is ", total


Even Numbers Total
Write pseudocode to:
 Input numbers
 Add only even numbers to a total
 Stop when the user enters 0
 Output the total

DECLARE number, total : INTEGER


SET total ← 0

REPEAT
OUTPUT "Enter a number (0 to stop):"
INPUT number

IF number MOD 2 = 0 AND number <> 0 THEN


total ← total + number
ENDIF

UNTIL number = 0

OUTPUT "Total of even numbers = ", total


Cinema Ticket Booking
A cinema charges:
 Adult ticket = $12.00

 Child ticket = $8.00

If the total number of tickets is 5 or more, a 10% discount is applied.


Pseudocode
DECLARE AdultTickets: INTEGER
DECLARE ChildTickets: INTEGER
DECLARE Total: INTEGER
OUTPUT "Enter number of adult tickets"
INPUT AdultTickets
OUTPUT "Enter number of child tickets"
INPUT ChildTickets
Total ← (AdultTickets * 12) + (ChildTickets * 8)
IF (AdultTickets + ChildTickets) >= 5
THEN Total ← Total - (Total * 0.1)
ENDIF
OUTPUT "Total amount to pay is ", Total
Theme Park Entry
A theme park charges:
 Adult entry = $25

 Child entry = $18

If the total cost exceeds $100, a 20% discount is applied.


Pseudocode
OUTPUT "Enter number of adults"
INPUT Adults
OUTPUT "Enter number of children"
INPUT Children

Total ← (Adults * 25) + (Children * 18)

IF Total > 100 THEN


Total ← Total - (Total * 20 / 100)
ENDIF

OUTPUT "Final cost is ", Total


6th March, 2026

Write pseudocode to:


 Ask the user to enter a number.

 If the user enters 0, the program should stop.

 Otherwise, the program should count how many numbers are

between 10 and 20.


 It should also calculate the total of those numbers.

 Finally, display the count and the total.

DECLARE number, total, count : INTEGER


total ← 0
count ← 0
REPEAT
OUTPUT "Enter a number (0 to stop):"
INPUT number
IF number >= 10 AND number <= 20
THEN
total ← total + number
count ← count + 1
ENDIF
UNTIL number = 0
OUTPUT "Count of numbers between 10 and 20 = ", count
OUTPUT "Total of numbers between 10 and 20 = ", total
Error 1: Incorrect Initial Value of Small
Given code
Small = 0
❌ If all numbers entered are positive, Small will always stay 0, which is incorrect.
✔ Correction
INPUT Num
Small = Num
Counter = 1
(or initialize Small with the first number entered)

Error 2: Wrong Assignment in IF Statement


Given code
IF Num < Small THEN Num = Small
❌ This assigns the wrong variable.
✔ Correction
IF Num < Small THEN
Small = Num
ENDIF

Error 3: Output Statement in Wrong Place


Given code
PRINT Small
❌ This prints the value inside the loop every time.
✔ Correction
Use keyword OUTPUT and Move it after the loop:
OUTPUT Small
(after UNTIL)

Error 4: Incorrect UNTIL Condition


Given code
UNTIL Counter < 10
❌ This condition is incorrect because it will stop too early.
✔ Correction
UNTIL Counter = 10

CORRECTION
INPUT Num
Small = Num
Counter = 1

REPEAT
INPUT Num
IF Num < Small THEN
Small = Num
ENDIF
Counter = Counter + 1
UNTIL Counter = 10

OUTPUT Small
FOR LOOP
A FOR loop is a count-controlled loop that repeats a set of instructions a specific number of
times using a loop counter.
➡ Used when the number of repetitions is known in advance.
Syntax
FOR counter ← start_value TO end_value
statements
NEXT counter
 counter → loop variable that changes each time
 start_value → where the loop begins
 end_value → where the loop stops
 NEXT counter → increases the counter by 1 automatically

Example 1: Print numbers 1 to 5


DECLARE num: INTEGER
FOR num ← 1 TO 5
OUTPUT num
NEXT num
Output
1
2
3
4
5
Explanation:
The loop runs 5 times and prints numbers from 1 to 5.

Example 2: Print the word Pakistan 6 times

DECLARE count: INTEGER


FOR count  1 TO 6
OUTPUT “Pakistan”
NEXT count
Output
Pakistan
Pakistan
Pakistan
Pakistan
Pakistan
Pakistan
Individual Task
Write a pseudocode to generate following output

DECLARE num: INTEGER

FOR num ← 1 TO 10
OUTPUT num, " square = ", num * num
NEXT num

Question: Write Pseudocode that:


That creates the output of table of 2

DECLARE num: INTEGER


DECLARE count: INTEGER
num ← 2
FOR count ← 1 TO 10
OUTPUT num, "x", count, "=", num * count
NEXT count

Displaying all the even numbers between 12 and 81

FOR num ← 12 TO 81

IF num MOD 2 =0 THEN

OUTPUT num

ENDIF

NEXT num

Sum and Average of 10 Numbers

Pseudocode Solution

DECLARE total: REAL


DECLARE num:REAL
DECLARE average:REAL

total ← 0
FOR count ← 1 TO 10
INPUT num
total ← total + num
NEXT count

average ← total / 10

OUTPUT total
OUTPUT average

Write pseudocode to display all even numbers between 56 and 100.

Write pseudocode to input 15 numbers and calculate their sum and average.

You might also like