need help writing pseudo code
if (x > y) // either true of false
output x , “ is greater than “, y
else
output y, “ is greater than “, x
Relational operators (comparing)
== a = 9 if (a == 9
> ( 9 > 3)
>= (9 >= 3) // 9 is greater than 3 ( 9 >= 33) ( 9 >= 9)
< ( 9 < 3) // false
<= ( 9 <= 13) // True
<> or != Not equal to ( 9 <> 3) (9!=3)
string sign
input sign//
If (sign == “Green”)
output “Move forward”
elseif (sing == “Red”)
output “Stop”
else
output “Either stop or drive through”
End if
output “outside the if block”
num score
string grade
input “Enter your score: “, score
if (score >= 90)
grade = “A”
elseif (score >= 80)
grade = “B”
elseif (score >= 70)
grade = “C”
elseif (score >= 60)
grade = “D”
else
grade = “F”
end if
output “your score was “, score, “, therefore your grade is “, grade
Write a pseudo code to read three prices and test their subtotal. If subtotal >= 5000, then the discount = .10, else if the subtotal >= 2500 and < 5000, then the discount = .07, otherwise the discount = .05
Start
num price1, price2, price3, subtotal, taxAmount, total, discount, discountedAmount
num TAXRATE = .0825
ouput “Please enter your three prices: “
input price1, price2, price3
//calculation
subtotal = price1 + price2 + price3
if (subtotal >= 500) then
discount = .10
elseif (subtotal >= 2500) then
discount = .07
else
discount = .05
end if
discountedAmount = subtotal – (subtotal * discount)
TaxAmount = discountedAmount * TAXRATE
total = discountedAmount + TaxAmount
output “Your original subtotal = $”, subtotal
output “Your discounted Amount = $”, discountedAmount
output “Your discount = ”, discount * 100, “%”
output “Your tax amount = $”, taxAmount
output “Your total = $”, total
Stop
income = 25000 dep = 10
if (income <= 20000 AND dep >= 4) then
taxRate = .12
else
taxRate = .25
end if
Truth Table for AND === &&
|
T |
AND |
T |
|
T |
|
T |
AND |
F |
|
F |
|
F |
AND |
T |
|
F |
|
F |
AND |
F |
|
F |
income = 25000 dep = 10
if (income <= 20000 OR dep >= 4) then
taxRate = .12
else
taxRate = .25
end if
|
T |
OR |
T |
|
T |
|
T |
OR |
F |
|
T |
|
F |
OR |
T |
|
T |
|
F |
OR |
F |
|
F |
Not is unary
Not (T) = F
Not (F) = T
9 <> 3 that is true
Not(9)
Revisiting the score & grade program using logical opertors
num score
string grade
string flag = “F”
input “Enter your score: “, score
if (score < 0 OR score >=100) then
flag = “T”
elseif (score >= 90)
grade = “A”
elseif (score >= 80)
grade = “B”
elseif (score >= 70)
grade = “C”
elseif (score >= 60)
grade = “D”
else
grade = “F”
end if
if (flag == “T”) then
output “There is no such a score”
else
output “your score was “, score, “, therefore your grade is “, grade
end if
Looping – iteration
num index
for (index = 1 ; index <= 10; index = index + 1)
{
output “Hello world”
}
for index = 1 to 10
output “Hello, Shaw!”
next index
num jar = 0, index
for (index=1; index <=5; index++) //index ++ index = index + 1
{
jar = jar + index
}
ouput index
output sum
output sum/ (index-1)