Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A café screen shows three meals: 1 for soup, 2 for pasta and 3 for salad. Write pseudocode that inputs the customer's choice as an integer and outputs the name of the meal. Any other number outputs Not on the menu. Use a CASE statement.
IF <condition> then THEN, an optional ELSE, and ENDIF; THEN and ELSE are two spaces in. CASE OF <identifier> lists single values, each with one statement, tested in order; OTHERWISE comes last; ENDCASE closes it. Relational operators: =, <, <=, >, >=, <>. Logical: AND, OR, NOT.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Input the choice, then one case for each meal and an OTHERWISE for everything else.
DECLARE Choice : INTEGER
INPUT Choice
CASE OF Choice
1 : OUTPUT "Soup"
2 : OUTPUT "Pasta"
3 : OUTPUT "Salad"
OTHERWISE OUTPUT "Not on the menu"
ENDCASECheck. The input 2 matches the second case, so the output is Pasta. The input 7 matches no case, so OTHERWISE runs and the output is Not on the menu.
What if? With an IF statement you would need IF Choice = 1, then a nested IF for 2, then another for 3. The CASE statement is shorter and easier to read.
Log in to join the discussion.
Loading…