KEMBAR78
JW Pseudocode | PDF | Mathematical Logic | Computer Science
0% found this document useful (0 votes)
4 views9 pages

JW Pseudocode

Pseudocode is a method for describing algorithms using plain English and programming-like syntax, making it easier for humans to understand the logic before coding. It includes style rules for writing, such as using capital letters for keywords and indentation for clarity, as well as various structures like assignment statements, conditional statements, and loops. The document provides examples and practice questions to illustrate how to implement pseudocode for different tasks.

Uploaded by

Abdullateef Abba
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)
4 views9 pages

JW Pseudocode

Pseudocode is a method for describing algorithms using plain English and programming-like syntax, making it easier for humans to understand the logic before coding. It includes style rules for writing, such as using capital letters for keywords and indentation for clarity, as well as various structures like assignment statements, conditional statements, and loops. The document provides examples and practice questions to illustrate how to implement pseudocode for different tasks.

Uploaded by

Abdullateef Abba
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/ 9

Pseudocode

Pseudocode is a way to describe an algorithm (a step-by-step process to solve a problem) using


plain English mixed with programming-like keywords.
It’s not a real programming language — it only pretends to be code so humans can understand the
logic before writing actual code.

Example:

INPUT Name
OUTPUT "Hello ", Name

This means:

1. Ask the user for their name.

2. Show a greeting using their name.

2. How Pseudocode is Written (Style Rules)

In IGCSE style:

 Keywords like INPUT, OUTPUT, IF are in CAPITALS.

 Variable names start with a capital letter.

 Indent lines inside conditions or loops.

3. Assignment Statements (Giving Variables a Value)

The arrow ← is used to store a value in a variable.


The left side is the variable, the right side is the value or formula.

Mathematical operators:

 Add

 Subtract

 Multiply
/ Divide
^ Raise to a power
( ) Group operations

Examples:

Cost ← 10
Price ← Cost * 2
Tax ← Price * 0.12
SellingPrice ← Price + Tax
Gender ← "M"
Chosen ← False

4. Conditional Statements (Making Decisions)

a) IF … THEN … ELSE

Runs one block if the condition is TRUE, otherwise runs another.

IF Age < 18
THEN
OUTPUT "Child"
ELSE
OUTPUT "Adult"
ENDIF

b) CASE OF … OTHERWISE

Checks a variable against many possible values.

CASE OF Grade
"A" : OUTPUT "Excellent"
"B" : OUTPUT "Good"
"C" : OUTPUT "Average"
OTHERWISE OUTPUT "Improvement needed"
ENDCASE

5. Loops (Repeating Actions)

a) FOR … TO … NEXT

Repeat a set number of times.

FOR Counter ← 1 TO 5
OUTPUT Counter
NEXT Counter

b) REPEAT … UNTIL

Always runs at least once, then stops when the condition is TRUE.

Counter ← 0
REPEAT
OUTPUT Counter
Counter ← Counter + 1
UNTIL Counter = 3

c) WHILE … DO … ENDWHILE

Runs only while the condition is TRUE; might not run at all.

Counter ← 0
WHILE Counter < 3 DO
OUTPUT Counter
Counter ← Counter + 1
ENDWHILE

6. Input and Output

INPUT is for getting data from the user.


OUTPUT is for showing data to the user.

INPUT Name
OUTPUT "Hello ", Name

7. Explaining the Purpose of an Algorithm

Always ask:

1. What is it trying to achieve? (Purpose)

2. What steps does it follow? (Process)

Example:

REPEAT
Wait(10)
Get(Time)
UNTIL Time = Alarm_Time
OUTPUT AlarmSound

Purpose: Wait until the current time matches the alarm time, then play the alarm sound.
Practice Question

Write pseudocode for:

 Ask for a person’s age.

 If the age is less than 13, output "Child".

 If between 13 and 19, output "Teen".

 Otherwise, output "Adult".

Approach:

1. Input: Age

2. Conditions:

o Age < 13 → Child

o 13 ≤ Age ≤ 19 → Teen

o Else → Adult

3. Use IF … ELSEIF … ELSE

Solution:

INPUT Age
IF Age < 13
THEN
OUTPUT "Child"
ELSEIF Age <= 19
THEN
OUTPUT "Teen"
ELSE
OUTPUT "Adult"
ENDIF
More Practice Questions

1. Basic Assignment

Task: Ask the user for two numbers. Find their sum, product, and difference, then output the
results.

Approach:

 Get two numbers using INPUT.

 Use assignment (←) to calculate sum, product, and difference.

 OUTPUT the results.

Solution:
INPUT Num1
INPUT Num2
Sum ← Num1 + Num2
Product ← Num1 * Num2
Difference ← Num1 - Num2
OUTPUT "Sum = ", Sum
OUTPUT "Product = ", Product
OUTPUT "Difference = ", Difference

2. Simple IF Statement

Task: Ask for a test score, output "Pass" if ≥ 50, else "Fail".

Approach:

 INPUT score.

 Compare with 50.

 Use IF … ELSE.

Solution:
INPUT Score
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
3. IF … ELSEIF … ELSE

Task: Categorise temperature.

Approach:

 INPUT temperature.

 Check in order: <0 → Freezing, 0–20 → Cold, 21–30 → Warm, >30 → Hot.

Solution:
INPUT Temperature
IF Temperature < 0
THEN
OUTPUT "Freezing"
ELSEIF Temperature <= 20
THEN
OUTPUT "Cold"
ELSEIF Temperature <= 30
THEN
OUTPUT "Warm"
ELSE
OUTPUT "Hot"
ENDIF

4. CASE Statement

Task: Number 1–5 → words, else “Invalid number”.

Approach:

 INPUT number.

 Use CASE OF … OTHERWISE.

Solution:
INPUT Number
CASE OF Number
1 : OUTPUT "One"
2 : OUTPUT "Two"
3 : OUTPUT "Three"
4 : OUTPUT "Four"
5 : OUTPUT "Five"
OTHERWISE OUTPUT "Invalid number"
ENDCASE
5. FOR Loop

Task: Display numbers from 1 to N.

Approach:

 INPUT N.

 Use FOR Counter ← 1 TO N.

Solution:
INPUT N
FOR Counter ← 1 TO N
OUTPUT Counter
NEXT Counter

6. REPEAT Loop

Task: Keep asking for numbers until user enters 0.

Approach:

 Use REPEAT … UNTIL Number = 0.

Solution:
REPEAT
INPUT Number
UNTIL Number = 0
OUTPUT "Finished"

7. WHILE Loop

Task: Keep asking for a password until “secret” is entered.

Approach:

 INPUT password.

 WHILE password is not "secret", ask again.

Solution:
INPUT Password
WHILE Password <> "secret" DO
OUTPUT "Wrong password, try again"
INPUT Password
ENDWHILE
OUTPUT "Access granted"
8. Nested IF

Task: Check gender and age to give category.

Approach:

 INPUT Gender and Age.

 First check Gender, then check Age inside each gender branch.

Solution:
INPUT Gender
INPUT Age
IF Gender = "M"
THEN
IF Age >= 18
THEN
OUTPUT "Adult Male"
ELSE
OUTPUT "Boy"
ENDIF
ELSEIF Gender = "F"
THEN
IF Age >= 18
THEN
OUTPUT "Adult Female"
ELSE
OUTPUT "Girl"
ENDIF
ENDIF

9. Calculations with Conditions

Task: Calculate normal and overtime pay.

Approach:

 INPUT Hours and Rate.

 If Hours > 40 → calculate normal pay for first 40, overtime for extra hours.

 Else → normal pay only.

Solution:
INPUT Hours
INPUT Rate
IF Hours > 40
THEN
NormalPay ← 40 * Rate
OvertimeHours ← Hours - 40
OvertimePay ← OvertimeHours * Rate * 1.5
TotalPay ← NormalPay + OvertimePay
ELSE
TotalPay ← Hours * Rate
ENDIF
OUTPUT "Total pay = ", TotalPay

You might also like