You are on page 1of 10

SELECTION

(MULTIPLE SELECTION) –
more than 2 choices

Format for Pseudocode :

START
INPUT
IF condition1
statement1
ELSE IF condition2
statement2
.
.
.
ELSE IF condition n
statement n
ELSE
last statement
END IF
STOP
1. Write a program that will accept a number. Display “Negative number” if
the number is less than 0. If the number is greater than 0, display “Positive
number”. Otherwise, display “The number is 0”.
number < 0 – “Negative number”
number > 0 – “Positive number”
“The number is 0”

IPO :
INPUT PROCESS OUTPUT
number determine number less than 0 or “Negative number” OR
not to identify positive, negative “Positive number” OR
or zero “The number is 0”
2. Suppose that temperature is an int variable representing the
temperature of an oven. Write a program that output the phrases
below based on temperature input from the user
≤ 200
Value of temperature Phrase to print
Not over 200 IF Too cold to cook
≤ 350 > 200 , ≤ 350
Over 200 but not over 350 ELSE IF Oven is preheating
≤ 425 > 350 , ≤ 425
Over 350 but not over 425 ELSE IF Put in the apple pie
≤ 500 > 425 , ≤ 500 Over 425 but not over 500 ELSE IF Time to cook pizza
>500 Over 500 ELSE Call the fire department

IPO :
INPUT PROCESS OUTPUT
temperature determine “Too cold to cook” OR
temperature less than “Oven is preheating” OR
or equal to 200 or not “Put in the apple pie” OR
to identify the phrases
“Time to cook pizza” OR
“Call the fire department”

Pseudocode : IF temperature > 200 and ≤ 350


START IF temperature ≤ 350
READ temperature
IF temperature ≤ 200
PRINT “Too cold to cook”
ELSE IF temperature > 200 and temperature ≤ 350
PRINT “Oven is preheating”
ELSE IF temperature > 350 and temperature ≤ 425
PRINT “Put in the apple pie”
ELSE IF temperature > 425 and temperature ≤ 500
PRINT “Time to cook pizza”
ELSE
PRINT “Call the fire department”
END IF
STOP

FLOWCHART (HOMEWORK)
3. Write a program that will display the amount of commission that
a worker will get based on the sales achieved.
Sales Commission Rate
< 10000
Less than RM 10,000 IF 10% 0.1
≤ 15000 ≥ 10000 - ≤ 15000
RM 10,000 – RM 15,000 ELSE IF 15% 0.15
>15000 Over RM 15,000 ELSE 20% 0.2

IPO :
INPUT sales
PROCESS determine sales less than 10000 or not then calculate amount
of commission based on sales
OUTPUT amount of commission

Pseudocode :
START
READ sales
IF sales < 10000
amount of commission = sales x 0.1
ELSE IF sales ≤ 15000
amount of commission = sales x 0.15
ELSE
amount of commission = sales x 0.2
END IF
DISPLAY amount of commission
STOP

FLOWCHART (HOMEWORK)
4. A courier company, GoFast want to use a computer program to determine
payment of their mileage claims. Their programmer ask for your help to
write the program. The payment is calculated using this table.
KM Cents per KM
First 500 50
Next 200 40
Each extra km 30

Example :
a. If the travel took 100 km, they should pay RM50.00
b. If the travel took 600 km, they should pay RM290.00 (that is 250 + 40)
c. If the travel took 800 km, they should pay RM360.00 (that is 250 + 80 + 30)

IPO, PSEUDOCODE (HOMEWORK)


VARIABLE – location in computer memory used to hold or store value
Bila variable wujud? – bila program ada input (READ / INPUT / GET)

Macam mana nak tau mana satu variable? Tengok pada :


1. Input (READ / INPUT / GET)
2. Formula / process
3. Output without “ “

PRINT sum – sum is a variable


PRINT “sum” - sum is not a variable

Question 1 : The program will display “Hello World” – no variable


START
PRINT “Hello World” System.out.print(“Hello World”);
STOP

Question 2 : Accept and display student name – 1 variable only


START
READ student name student_name = sc.nextLine( );
PRINT student name System.out.print(student_name);
STOP

Question 3 : The program will calculate and display sum of 2 numbers – 3


variables
START
READ num1, num2 num1=sc.nextInt( ); num2=sc.nextInt( );
sum = num1 + num2 sum = num1 + num2;
PRINT sum System.out.print(sum);
STOP
System.out.print(“Sum is “+sum);

Assume value for sum is 250


Java statement Output
System.out.print(sum); 250
System.out.print(“Sum is “+sum); Sum is 250
__________________
sum, sum – 1 variable
Sum, sum - 2 variables
____________________

Question 4 : The program will display sum and average of 4 numbers – 6


variables
START
READ num1, num2, num3, num4
sum = num1 + num2 + num3 + num4
average = sum ÷ 4
PRINT sum, average
STOP

Find the output for this Java coding.

Coding 1 :
System.out.print(“KMPk”);
System.out.print(“Gopeng”);

Output :

KMPkGopeng

Coding 2 :
System.out.println(“KMPk”);
System.out.print(“Gopeng”);

Output :

KMPk
Gopeng
Coding 3 :

int age = 18;


System.out.print(age);
System.out.print(“age”);

Output :

18age

Coding 4 :
String location = “Gopeng”;
System.out.print(“location”);
System.out.print(location);

Output :
locationGopeng

Coding 5 :
String location = “Gopeng”;
System.out.print(“location ”);
System.out.print(location);

Output :
location Gopeng
Coding 6 :
int age = 18;
System.out.print(“My age is “+age);

Output :

My age is 18
User input : KMPk Gopeng

name=sc.next( ); - value utk name adalah


KMPk

name=sc.nextLine( ); - value utk name


adalah KMPk Gopeng

1. Create a variable name carName and assign the value Volvo to it


carName = “Volvo”;

2. Create a variable name maxSpeed and assign the value 120 to it


maxSpeed = 120;

3. Declare and create a variable name flag and assign the value ‘true’
to it
boolean flag = true;
4. Declare a variable name grade
char grade;

5. Create a variable name weight and assign the value 64.7 to it


weight = 64.7;

You might also like