NADF511 StudyNotesAndExamples Ch4to7
NADF511 StudyNotesAndExamples Ch4to7
NADF 511
Applications Development Foundations
Comprehensive Study Guide
Chapters 4 – 7 | Problem Solving · IPO · Pseudocode · Trace Tables
Sol Plaatje University | 2026
EXAMPLE 2: Is 16 = 5 true?
✓ ANSWER: FALSE – 16 does not equal 5
AND BOTH must be TRUE to yield TRUE T AND T=T | T AND F=F | F
AND F=F
Syntax:
if condition then
statement(s)
endif
Pseudocode:
CalcAmount
display "Number of pencils bought?"
enter number
display "Price of one pencil?"
enter price
amount = number * price
if number >= 25 then
amount = amount - (amount * 0.075)
endif
display "Amount due: R ", amount
end
display Price?
enter 30 2.25
if (30>=25) TRUE
Pseudocode:
PrepareResult
display "Enter mark: "
enter mark
message = "pass"
if mark < 50 then
message = "fail"
endif
display "Mark: ", mark, " Result: ", message
end
enter 45
assign 45 pass
if (45<50) TRUE
assign 45 fail
Pseudocode:
CalcWage
display "Hours worked?"
enter hours
display "Basic wage?"
enter wage
if hours > 45 then
wage = wage + 300
endif
display "Total wage: R ", wage
end
enter 50 900
if (50>45) TRUE
calculate 50 1200
Pseudocode:
RentalCost
enter days, kmRate, distance
cost = 200 * days + kmRate * distance
if distance < 50 then
Trace Table:
Instruction distance cost Output
enter 30
calculate 30 560
if (30<50) TRUE
calculate 30 588
Pseudocode:
CalcCakePrice
display "Cost of cake?"
enter expense
cakePrice = expense + expense * 0.3
sliceInt = cakePrice \ 10
sliceDec = cakePrice / 10
if sliceInt <> sliceDec then
sliceInt = sliceInt + 1
endif
display "Price per slice: R ", sliceInt
end
Trace Table:
Instruction expense cakePrice sliceInt sliceDec Output
enter 18.60
calc 24.18
calc 2
calc 2.418
if TRUE
(2<>2.418)
calc 3
display R3
Pseudocode:
CalcWages
display "Hours worked?"
enter hours
wage = hours * 16.50
if hours > 40 then
wage = wage + (hours - 40) * 10
endif
display "Total wages: R ", wage
end
Trace Table:
Instruction hours wage Output
enter 45
calc 45 742.50
if (45>40) TRUE
calc 45 792.50
display R792.50
IPO Chart:
INPUT PROCESSING OUTPUT
hours (Real) Enter hours and tariff wage (Real)
tariff (Real) Validate: if numeric
Calculate wage = tariff * hours
Else: display error
Pseudocode:
ValidateAndCalc
display "Hours worked?"
enter hours
display "Tariff per hour?"
enter tariff
if hours is numeric AND tariff is numeric then
wage = tariff * hours
display "Wage: R ", wage
else
display "Invalid input values"
endif
end
Trace Table:
Instruction hours tariff wage Output
enter abc
enter 15.00
if (numeric?) FALSE
Pseudocode:
CalcNetIncome
Trace Table:
Instruction deptNo bonus gross netIncome Output
enter 7
assign 7 500
if (7=7) TRUE
assign 7 550
calc 5550
calc 4440
display R4440
Pseudocode:
PizzaDecision
enter cost, jessie, sally
combined = jessie + sally
if combined >= cost then
display "Buy the large pizza!"
else
display "Buy a small pizza."
endif
end
Trace Table:
Instruction combined cost Output
calc 75
enter 80
if (75>=80) FALSE
Pseudocode:
ComputerTransport
enter total, capacity
if total > capacity then
remaining = total - capacity
display "Cannot transport all. Still need: ", remaining
else
display "All computers can be transported."
endif
end
Trace Table:
Instruction total capacity remaining Output
enter 20 15
if (20>15) TRUE
calc 5
if condition then
statements when TRUE
else
statements when FALSE
endif
Pseudocode:
DetermineWinner
enter pointA, pointF
if pointA > pointF then
winner = "Angelina"
pointW = pointA
else
winner = "Freedom"
pointW = pointF
endif
display "Winner: ", winner, " Points: ", pointW
end
Trace Table:
Instruction pointA pointF winner pointW Output
enter 175 179
if FALSE
(175>179)
assign Freedom 179
Pseudocode:
ReadingSpeed
enter bookPages, dayPages
bookDays = bookPages / dayPages
days = bookDays * 5
if days <= 14 then
display "Can read 5 books!"
else
display "Cannot read 5 books."
endif
end
Trace Table:
Instruction bookPages dayPages bookDays days Output
enter 120 50
calc 2.4
calc 12
if (12<=14) TRUE
Pseudocode:
CalcIncrease
enter dept, salary
if dept = "A" then
increase = salary * 0.1
else
increase = salary * 0.08
endif
salary = salary + increase
display "New salary: R ", salary
end
Trace Table:
Instruction dept salary increase Output
enter B 24000
if ('B'='A') FALSE
calc 1920
calc 25920
display R25920
Pseudocode:
CalcNetSalary
enter anSalary, empType
monthly = anSalary / 12
if empType = "full" then
tax = monthly * 0.295
else
tax = monthly * 0.25
endif
Trace Table:
Instruction monthly empType tax net Output
calc 4000
enter full
if ('full') TRUE
calc 1180
calc 2820
display R2820
Pseudocode:
CalcTickets
enter numTickets
if numTickets >= 8 then
total = numTickets * 12.50
else
total = numTickets * 15
endif
display "Amount due: R ", total
end
Trace Table:
Instruction numTickets total Output
enter 10
if (10>=8) TRUE
calc 125.00
display R125.00
Pseudocode:
HeavierDog
enter fluffy, terry
if fluffy > terry then
display "Fluffy is heavier"
else
display "Terry is heavier"
endif
end
Trace Table:
Instruction fluffy terry Output
enter 8.5 12.0
if (8.5>12.0) FALSE
Pseudocode:
CalcCourseFee
enter courseCode, weeks
if courseCode = "A" then
total = weeks * 234
else
total = weeks * 287.50
endif
display "Total fee: R ", total
end
Trace Table:
Instruction courseCode weeks total Output
enter B 4
if ('B'='A') FALSE
calc 1150
display R1150
EXAMPLE 8: Mark Adjustment – > 47: increase 2.5%, else decrease 5.7%
Problem: Enter student mark. If mark > 47, increase by 2.5%. Otherwise decrease by 5.7%.
IPO Chart:
INPUT PROCESSING OUTPUT
mark (Real) Enter mark mark (Real)
If mark > 47: mark = mark *
1.025
Else: mark = mark * 0.943
Display new mark
Pseudocode:
AdjustMark
enter mark
if mark > 47 then
mark = mark * 1.025
else
mark = mark * 0.943
endif
display "New mark: ", mark
end
Trace Table:
Instruction mark (in) mark (out) Output
enter 40
if (40>47) FALSE
calc 37.72
display 37.72
Problem: Enter employee salary. Only people earning > R3000/month pay tax.
IPO Chart:
INPUT PROCESSING OUTPUT
empSalary (Real) Enter salary message (String)
If empSalary > 3000: display tax
message
Else: display no tax message
Pseudocode:
TaxCheck
enter empSalary
if empSalary > 3000 then
display "Tax required"
else
display "No tax required"
endif
end
Trace Table:
Instruction empSalary Output
enter 2500
if (2500>3000) FALSE
EXAMPLE 10: Josie's Movie – Boolean: homework done AND has R10
Problem: Josie can go to movies if homework is done AND she has R10. Use a Boolean variable.
IPO Chart:
INPUT PROCESSING OUTPUT
homework (Boolean) Enter homework status and message (String)
money (Real) money
If homework AND money >= 10:
display 'Go to movies'
Else: display 'Stay home'
Pseudocode:
MoviePermission
enter homework, money
if homework AND money >= 10 then
display "You can go to the movies!"
else
display "Stay home."
endif
end
Trace Table:
Instruction homework money Output
enter TRUE 10
Pseudocode:
EntranceFee
enter fee, age
if age < 12 OR age >= 60 then
actFee = fee - fee * 0.25
else
actFee = fee
endif
display "Fee: R ", actFee
end
Trace Table:
Instruction fee age actFee Output
enter 50 9
if (9<12 OR TRUE
9>=60)
calc 37.50
display R37.50
EXAMPLE 2: Car Driver – Age >= 18 AND has licence → may drive
Problem: Person may drive only if aged 18+ AND has a driver's licence (Boolean).
IPO Chart:
INPUT PROCESSING OUTPUT
Pseudocode:
DriverCheck
enter age, licensed
if age >= 18 AND licensed then
display "You may drive a car."
else
display "You may not drive a car."
endif
end
Trace Table:
Instruction age licensed Output
enter 20 TRUE
Pseudocode:
EmployeeBonus
enter grade, years
if grade = "C" OR years > 5 then
bonus = 1000
else
bonus = 0
endif
display "Bonus: R ", bonus
end
Trace Table:
Instruction grade years bonus Output
enter A 7
if ('A'='C' OR TRUE
7>5)
assign 1000
display R1000
Pseudocode:
TebogoBudget
display "Enter monthly salary (< R1800):"
enter salary
if salary < 1800 AND salary > 0 then
remainder = salary - 450
food = remainder * 0.50
clothes = remainder * 0.20
transport = remainder * 0.15
charity = remainder * 0.05
pocket = remainder - food - clothes - transport - charity
display "Food: R", food
display "Clothes: R", clothes
display "Transport: R", transport
display "Charity: R", charity
display "Pocket: R", pocket
else
display "Invalid salary entered"
endif
end
Trace Table:
if (<1800 TRUE
AND >0)
calc 1250
calc 625
calc 250
calc 75
display Food:R625...
EXAMPLE 5: Tennis Court – Calculate Perimeter AND Area [from Sem Test]
Problem: Enter length and width of tennis court. Calculate and display perimeter and area.
IPO Chart:
INPUT PROCESSING OUTPUT
length (Real) Enter length and width perimeter (Real)
width (Real) perimeter = 2 * (length + width) area (Real)
area = length * width
Display perimeter and area
Pseudocode:
TennisCourt
display "Enter length in metres:"
enter length
display "Enter width in metres:"
enter width
if length is numeric AND width is numeric then
perimeter = 2 * (length + width)
area = length * width
display "Perimeter: ", perimeter, " m"
display "Area: ", area, " m²"
else
display "Invalid input"
endif
end
Trace Table:
Instruction length width perimeter area Output
enter 23.77 10.97
if TRUE
(numeric)
calc 69.48
calc 260.85
display 69.48m |
260.85m²
Pseudocode:
GymEntry
enter stName, member
if member then
message = "must pay R8"
else
message = "must pay R12"
endif
display stName, " ", message
end
Trace Table:
Instruction stName member message Output
enter Sally Jones TRUE
if (TRUE) TRUE
Pseudocode:
MedalCheck
enter cycle, swim
if cycle >= 20 AND swim >= 500 then
display "Qualified for medal!"
else
display "Not qualified."
endif
end
Trace Table:
Instruction cycle swim Output
enter 22 450
Pseudocode:
StudentDiscount
enter age, studentCard, price
if age < 18 OR studentCard then
price = price * 0.85
endif
display "Final price: R ", price
end
Trace Table:
Instruction age studentCard price Output
enter 25 TRUE 200
if (25<18 OR TRUE
TRUE)
calc 170
display R170
EXAMPLE 9: Commission – Sales < R3500: 8%, Sales >= R3500: 12.8%
Problem: Employee earns R1200 basic + commission. < R3500 sales = 8%, else = 12.8%. 5% goes to
manager.
IPO Chart:
INPUT PROCESSING OUTPUT
sales (Real) Enter sales netIncome (Real)
If sales < 3500: commission =
sales * 0.08
Else: commission = sales *
0.128
managerCut = commission *
0.05
netIncome = 1200 +
commission - managerCut
Pseudocode:
CalcIncome
enter sales
if sales < 3500 then
commission = sales * 0.08
else
commission = sales * 0.128
endif
managerCut = commission * 0.05
netIncome = 1200 + commission - managerCut
display "Net income: R ", netIncome
end
Trace Table:
Instruction sales commission managerCut netIncome Output
enter 4000
if FALSE
(4000<3500)
calc 512
calc 25.60
calc 1686.40
display R1686.40
IPO Chart:
INPUT PROCESSING OUTPUT
A (Real) Enter A and B A (Real)
B (Real) If A > 17 AND A < 47: swap B (Real)
using temp variable
Else if A = 5 OR A = 87: A =
A+20, B = B-5
Pseudocode:
SwapOrAdjust
enter A, B
if A > 17 AND A < 47 then
temp = A
A = B
B = temp
else
if A = 5 OR A = 87 then
A = A + 20
B = B - 5
endif
endif
display "A = ", A, " B = ", B
end
Trace Table:
Instruction A B temp Output
enter 25 10
assign 10
assign 25
Pseudocode:
AssignPoints
enter gender, age
if gender = "M" then
if age > 15 then
points = 17
else
points = 20
endif
else
if gender = "F" then
if age < 18 then
Sol Plaatje University | Page 30 of 57
NADF 511 – Applications Development Foundations | Chapters 4–7 Study Guide
points = 18
else
points = 21
endif
else
display "Invalid gender"
endif
endif
display "Points: ", points
end
Trace Table:
Instruction gender age points Output
enter F 19
if (F=M) FALSE
if (F=F) TRUE
if (19<18) FALSE
assign 21
display 21
Pseudocode:
CalcNewSalary
enter deptCode, anSalary
if anSalary is numeric then
monSalary = anSalary / 12
if deptCode = "A" then
monSalary = monSalary * 1.072
else
if deptCode = "B" then
monSalary = monSalary * 1.068
else
monSalary = monSalary * 1.063
endif
endif
Trace Table:
Instruction deptCode anSalary monSalary Output
enter A 24000
if numeric TRUE
calc 2000
if (A=A) TRUE
calc 2144
display R2144
Pseudocode:
ModifyValues
enter A, B
if A = 5.2 then
if B > 20 then
A = A + 5
else
B = B - 3.5
endif
else
if A = 6 then
A = 0
B = 0
endif
endif
display "A = ", A, " B = ", B
end
Trace Table:
Instruction A B Output
enter 5.2 25
if (A=5.2) TRUE
if (25>20) TRUE
calc 10.2
Pseudocode:
CheapStoreCalc
enter price, numItems, discPct
if price is numeric AND numItems is numeric then
subtotal = price * numItems
if numItems >= 10 then
discount = subtotal * discPct / 100
amountDue = subtotal - discount
display "Discount: R", discount
else
amountDue = subtotal
display "No discount applied"
endif
display "Amount due: R", amountDue
else
display "Invalid input"
endif
end
Trace Table:
Instruction numItems subtotal discount amountDue Output
enter 12
calc 360
if (12>=10) TRUE
calc 36
calc 324
display R324
Pseudocode:
NetIncome
enter sales, dept
if sales < 3500 then
commission = sales * 0.08
else
commission = sales * 0.128
endif
if dept = 7 then
bonus = 50
else
bonus = 0
endif
net = 1200 + commission + bonus - commission * 0.05
display "Net income: R ", net
end
Trace Table:
Instruction sales dept commission bonus net Output
enter 4000 7
if FALSE
(<3500)
calc 512
if TRUE
(dept=7)
assign 50
calc 1736.40
display R1736.40
Pseudocode:
AssignGrade
enter mark
if mark >= 75 then
grade = "A"
else
if mark >= 60 then
grade = "B"
else
if mark >= 50 then
grade = "C"
else
if mark >= 40 then
grade = "D"
else
grade = "F"
endif
endif
endif
endif
display "Grade: ", grade
end
Trace Table:
Instruction mark grade Output
enter 65
if (>=75) FALSE
if (>=60) TRUE
assign B
display Grade: B
IPO Chart:
INPUT PROCESSING OUTPUT
A (Real) Enter A, B, C largest (Real)
B (Real) Nested IF to compare and find
C (Real) largest
Pseudocode:
FindLargest
enter A, B, C
if A > B then
if A > C then
largest = A
else
largest = C
endif
else
if B > C then
largest = B
else
largest = C
endif
endif
display "Largest: ", largest
end
Trace Table:
Instruction A B C largest Output
enter 5 12 9
if (5>12) FALSE
if (12>9) TRUE
assign 12
display 12
Pseudocode:
RentalCost
enter carType, days
if carType = "A" then
rate = 250
else
rate = 180
endif
cost = rate * days
if days > 7 then
cost = cost * 0.90
endif
display "Total cost: R ", cost
end
Trace Table:
Instruction carType days cost Output
enter A 10
if (A=A) TRUE
calc 2500
if (10>7) TRUE
calc 2250
display R2250
Pseudocode:
TebogoBudgetFull
display "Enter monthly salary (must be less than R1800):"
enter salary
if salary > 0 AND salary < 1800 then
remainder = salary - 450
if remainder > 0 then
food = remainder * 0.50
clothes = remainder * 0.20
transport = remainder * 0.15
Trace Table:
Step salary remainder food pocket Output
enter 1600
if (>0) TRUE
calc 575
calc 115
display Food:R575
Pocket:R115
EXAMPLE 10: Select Case: Day Name – Map number 1-7 to day name
Problem: Enter a number 1-7. Display the corresponding day of the week using SELECT CASE.
IPO Chart:
INPUT PROCESSING OUTPUT
dayNum (Integer) Enter dayNum dayName (String)
Select case dayNum to assign
dayName
Pseudocode:
DayName
display "Enter day number (1-7):"
enter dayNum
select case dayNum
case 1: dayName = "Monday"
case 2: dayName = "Tuesday"
Trace Table:
Instruction dayNum dayName Output
enter 5
case 5 Friday
display Friday
Syntax:
for variable = begin-value to end-value [step value]
statement(s)
next variable
FOR-NEXT Loop Examples with IPO, Pseudocode & Trace Table (10)
EXAMPLE 1: Display 1 to 10 – Basic for-next loop
Problem: Display consecutive numbers from 1 to 10 on one line.
IPO Chart:
INPUT PROCESSING OUTPUT
(no input) for i = 1 to 10: display i i (Integer) displayed
Pseudocode:
Display1To10
for i = 1 to 10
display i, " "
next i
end
Trace Table:
i Output
1 1
2 2
3 3
... ...
10 10
Pseudocode:
Descend10To1
for i = 10 to 1 step -1
display i, " "
next i
end
Trace Table:
i Output
10 10
9 9
8 8
... ...
1 1
Pseudocode:
SumOddNumbers
sum = 0
odd = 1
for k = 1 to 5
sum = sum + odd
odd = odd + 2
next k
display "Sum of first 5 odds: ", sum
end
Trace Table:
k odd sum Output
1 1 1
2 3 4
3 5 9
4 7 16
5 9 25
done Sum=25
Pseudocode:
AverageOf8
sum = 0
for x = 1 to 8
display "Enter integer between 5 and 48:"
enter number
if number > 5 AND number < 48 then
sum = sum + number
else
display "Invalid - re-enter"
x = x - 1
endif
next x
average = sum / 8
display "Average: ", average
end
Trace Table:
x number sum Output
1 20 20
2 35 55
8 40 240
— — — Average: 30
Problem: Enter starting even number and how many even numbers to display. Display them.
IPO Chart:
INPUT PROCESSING OUTPUT
beginNo (Integer) Validate inputs evenNo (Integer)
howMany (Integer) Check beginNo is even (mod 2
= 0)
for x=1 to howMany: display
even, even+=2
Pseudocode:
DisplayEvens
enter beginNo, howMany
if beginNo is numeric AND howMany > 0 then
remainder = beginNo MOD 2
if remainder = 0 then
even = beginNo
for x = 1 to howMany
display even, " "
even = even + 2
next x
else
display "Start must be an even number"
endif
else
display "Invalid input"
endif
end
Trace Table:
x even Output
1 8 8
2 10 10
3 12 12
... ... ...
10 26 26
Pseudocode:
TestResults
display "Enter name of first student:"
enter stName
display "Enter test mark:"
enter testMark
highestName = stName
highest = testMark
lowestName = stName
lowest = testMark
for st = 2 to 10
enter stName, testMark
if testMark > highest then
highest = testMark
highestName = stName
else
if testMark < lowest then
lowest = testMark
lowestName = stName
endif
endif
next st
display "Highest: ", highestName, " - ", highest
display "Lowest: ", lowestName, " - ", lowest
end
Trace Table:
st stName mark highest lowest Output
1 Danny 50 50 50
2 Bill 67 67 50
3 Don 92 92 50
4 Dave 28 92 28
done Don:92
Dave:28
Pseudocode:
TimesTable
tableNum = 5
for i = 1 to 10
result = tableNum * i
display tableNum, " x ", i, " = ", result
next i
end
Trace Table:
i result Output
1 5 5 x 1 = 5
2 10 5 x 2 = 10
3 15 5 x 3 = 15
... ... ...
10 50 5 x 10 = 50
Pseudocode:
DivisionBySubtract
enter totNumber
if totNumber > 400 AND totNumber is numeric then
remainder = totNumber
result = 0
do while remainder >= 5
remainder = remainder - 5
result = result + 1
loop
display "Result of ", totNumber, " / 5 = ", result
else
Trace Table:
Step totNumber remainder result Output
enter 425
init 425 0
loop 1 420 1
loop 2 415 2
loop 85 0 85
display 425/5=85
Pseudocode:
SumEvens
sum = 0
for i = 2 to 20 step 2
sum = sum + i
next i
display "Sum of even numbers 2-20: ", sum
end
Trace Table:
i sum Output
2 2
4 6
6 12
... ...
20 110
— — Sum=110
EXAMPLE 10: Count Integers into Sum – Consecutive integers from 24 [Sem Test 2.2]
Problem: Calculate sum of consecutive integers starting at 24 while sum < 23456. Display how many
integers were added.
IPO Chart:
INPUT PROCESSING OUTPUT
(no input – uses sentinel logic) sum=0, number=24, count=0 count (Integer)
do while sum < 23456:
sum += number, number++,
count++
Display count
Pseudocode:
CalcSum
sum = 0
number = 24
count = 0
do while sum < 23456
sum = sum + number
number = number + 1
count = count + 1
loop
display "Number of integers added: ", count
end
Trace Table:
count number sum Output
0 24 0
1 25 24
2 26 49
3 27 75
Pseudocode:
FishingWinner
winnerNumber = 0
display "Fish count (-1 to stop):"
enter noFish
do while noFish <> -1
enter fmName
if noFish > winnerNumber then
winnerNumber = noFish
winner = fmName
endif
Trace Table:
noFish fmName winnerNumber winner Output
8 Sam 8 Sam
2 Johnny 8 Sam
7 Kevin 8 Sam
10 Fred 10 Fred
12 Ted 12 Ted
9 Paul 12 Ted
Pseudocode:
AlexisShopping
total = 0
display "Money in purse: "
enter purseMoney
display "Enter price (0 to stop):"
enter price
do while price <> 0
total = total + price
display "Next price (0 to stop):"
enter price
loop
if total > 100 then
total = total - total * 0.035
endif
if purseMoney >= total then
change = purseMoney - total
display "Change: R ", change
else
shortMoney = total - purseMoney
display "Need R ", shortMoney, " more"
endif
end
Trace Table:
price total Discount? purseMoney Output
45 45 150
30 75
35 110
Pseudocode:
TicketSales
noTickets = 100
do
display "Tickets to buy?"
enter ticketsToBuy
if ticketsToBuy <= noTickets then
noTickets = noTickets - ticketsToBuy
display "Sold. Remaining: ", noTickets
else
display "Only ", noTickets, " available"
endif
loop until noTickets = 0
display "All tickets sold!"
end
Trace Table:
ticketsToBuy noTickets Output
30 70 Sold. Remaining:70
40 30 Sold. Remaining:30
30 0 Sold. Remaining:0
stop 0 All tickets sold!
EXAMPLE 4: Sum of Consecutive Integers from 24 – Pre-test loop [Sem Test 2.2]
Problem: Calculate sum of consecutive integers starting at 24 while sum < 23456. Display count.
IPO Chart:
INPUT PROCESSING OUTPUT
(no user input) sum=0, number=24, count=0 count (Integer)
do while sum < 23456:
sum+=number, number++,
count++
Display count
Pseudocode:
CalcSum
sum = 0
number = 24
count = 0
do while sum < 23456
sum = sum + number
number = number + 1
count = count + 1
loop
display "Integers added: ", count
end
Trace Table:
Loop number sum count Output
1 24 24 1
2 25 49 2
3 26 75 3
Pseudocode:
ValidSalesInput
do
display "Enter sales amount (must be >= 0):"
enter salesAmount
if NOT (salesAmount is numeric AND salesAmount >= 0) then
display "Invalid. Please re-enter."
endif
loop until salesAmount is numeric AND salesAmount >= 0
display "Valid amount entered: R ", salesAmount
end
Trace Table:
Attempt salesAmount Valid? Output
1 abc FALSE Invalid. Re-enter.
2 -5 FALSE Invalid. Re-enter.
3 150 TRUE Valid: R150
Pseudocode:
HighestSales
highestSales = 0
display "Enter sales amount (-1 to stop):"
enter salesAmount
do while salesAmount <> -1
if salesAmount > highestSales then
highestSales = salesAmount
endif
display "Next amount (-1 to stop):"
enter salesAmount
loop
display "Highest sales: R ", highestSales
end
Trace Table:
salesAmount highestSales Output
500 500
1200 1200
800 1200
350 1200
EXAMPLE 7: Name and Age Entry – Repeat for multiple people until stop
Problem: Enter name and age for people. Use 'STOP' as sentinel for name. Display count entered.
IPO Chart:
INPUT PROCESSING OUTPUT
name (String) count=0 count (Integer)
age (Integer) Enter first name
do while name <> 'STOP':
enter age, count++
display name and age
enter next name
Display count
Pseudocode:
PeopleEntry
count = 0
display "Enter name (STOP to quit):"
enter name
do while name <> "STOP"
display "Enter age:"
enter age
count = count + 1
display count, ": ", name, " - ", age
display "Enter next name (STOP to quit):"
enter name
loop
display "Total people entered: ", count
end
Trace Table:
name age count Output
Alice 25 1 1: Alice - 25
Bob 30 2 2: Bob - 30
STOP — 2 Total: 2
Pseudocode:
PositiveNumber
do
display "Enter a positive number:"
enter number
if number <= 0 then
display "Must be positive. Try again."
endif
loop until number > 0
display "You entered: ", number
end
Trace Table:
Attempt number Valid? Output
1 0 FALSE Must be positive.
2 -3 FALSE Must be positive.
3 7 TRUE You entered: 7
Pseudocode:
ATMWithdrawal
display "Account balance: "
enter balance
display "Amount to withdraw:"
enter amount
do while amount > balance OR amount <= 0
display "Invalid amount. Enter between R1 and R", balance
enter amount
loop
balance = balance - amount
display "New balance: R ", balance
end
Trace Table:
amount balance Valid? Output
0 1000 FALSE Invalid amount
1200 1000 FALSE Invalid amount
500 1000 TRUE
EXAMPLE 10: Monthly Salary Categories – Full Tebogo problem with do-loop
Problem: Full budget program for Tebogo. Use a do-loop to keep prompting until salary is valid (<
R1800 and > 0).
IPO Chart:
INPUT PROCESSING OUTPUT
salary (Real) do: food
enter salary clothes
loop until salary > 0 AND salary transport
< 1800 charity
remainder = salary - 450 pocket
Pseudocode:
TebogoBudgetLoop
do
display "Enter monthly salary (must be > 0 and < R1800):"
enter salary
if NOT (salary > 0 AND salary < 1800) then
display "Invalid salary. Please re-enter."
endif
loop until salary > 0 AND salary < 1800
remainder = salary - 450
food = remainder * 0.50
clothes = remainder * 0.20
transport = remainder * 0.15
charity = remainder * 0.05
pocket = remainder * 0.10
display "Rent: R450"
display "Food: R", food
display "Clothes: R", clothes
display "Transport: R", transport
display "Charity: R", charity
display "Pocket money: R", pocket
end
Trace Table:
Attempt salary Valid? food pocket Output
1 2000 FALSE Invalid.
Re-enter.
2 1500 TRUE
Pseudocode Keywords
Keyword Purpose
display Show output to screen
enter Read input from user
if / endif Simple conditional
if / else / endif Two-way conditional
for / next Fixed count loop
do while / loop Pre-test (unknown count) loop
do / loop until Post-test loop (runs at least once)
mod Returns remainder of division (17 mod 6 = 5)
\ Integer division (17 \ 6 = 2)
AND / OR / NOT Logical operators
is numeric Data validation test