Selection Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Write a program that lets the user input the speed of a car and outputs:

- “Too slow if the speed is between 0 and 40km/hr”


- Safe if the speed is between 41 and 100”
- “Too fast if the speed is between 101 and 150”
- “very dangerous is the speed is above 150”

Solution 1: IF…THEN…ELSE…END IF

START
DECLARE CarSpeed : REAL
INPUT “Enter the speed of the car”, CarSpeed
IF CarSpeed>=0 AND CarSpeed<=40 THEN
OUTPUT “Too slow”
ELSE IF CarSpeed>=41 AND CarSpeed<=100 THEN
OUTPUT “Safe Speed”
ELSE IF CarSpeed>=101 AND CarSpeed<=150 THEN
OUTPUT “Too fast”
ELSE IF CarSpeed>150 THEN
OUTPUT “Very dangerous speed”
ELSE
OUTPUT “Error speed cant be less than 0”
END IF
END

Solution 2: Using CASE…OF…OTHERWISE…END CASE

START
DECLARE CarSpeed : REAL
INPUT “Enter the speed of the car”, CarSpeed
CASE CarSpeed OF:
0 TO 40:
OUTPUT “Too slow”
41 TO 100:
OUTPUT “Safe Speed”
101 TO 150:
OUTPUT “Too fast”
>150:
OUTPUT “Very dangerous speed”
OTHERWISE:
OUTPUT “Error speed cant be less than 0”
END CASE
END
Example 2: Write a programme to ask the user to input their age and output as follows:
“Go to pre school”if age is below 0 to 5years
“Go to primary school”if age is between 5 and 11 years
“Go to high school” – 12 to 20 yrs
“Go to University”- 21 to 30 years
“Look for a job”- 31 to 65years
“Retire uyende kumusha”- 65 to 100years
“You have done you part on earth you can RIP”-greater than 100years

CASE…OF…OTHERWISE…ENDCASE
START
DECLARE age : INTEGER
CASE age OF:
0 TO 5 :
OUTPUT “go to preschool’’
5 TO 11:
OUTPUT ”go to primary school’’
12 TO 20:
OUTPUT ‘’go to highschool’’
21 TO 30:
OUTPUT ‘’go to university’’
31 TO 65:
OUTPUT ‘’look for a job’’
66 TO 100:
OUTPUT “retire uyende kumusha’’
>100:
OUTPUT ‘’RIP’’
OTHERWISE
OUTPUT” error , you do not exist”
ENDCASE
END
IF…THEN…ELSE…ENDIF

START
DECLARE age: INTEGER
INPUT “Enter your age”
IF age is >=0 AND age <=5 THEN
OUTPUT “Go to preshool”
ELSE IF age is >=6 AND age <=11 THEN
OUTPUT “Go to primary school”
ELSE IF age is >=12 AND age <=20 THEN
OUTPUT “Go to highshool”
ELSE IF age is >=21 AND age <=30 THEN
OUTPUT “Go to

You might also like