Upload
kris-jenine-junio
View
226
Download
0
Embed Size (px)
Citation preview
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 1/6
1
Lab Exercise 2: Multi-Way Selection
CS2 2010 – 2011
Instructions
• Get a partner and follow the steps below and answer each question to the best of your abilities
• Submit a single 1 whole sheet of Pad Paper with both your class numbers• Feel free to use the previous Lecture Slides and the Internet to search for the answers
when you get stuck
Objectives
1. To be familiar with the syntax of the two methods of implementing the Multi-WaySelection in C++
2. To understand the limitations and advantages of the two methods
Recall the Multi-way Selection…
• Used for comparing a variable to different possible values• Only one variable should be tested by all Decision Blocks – this variable is called the Control Variable
Let’s Begin!
There are codes below for you to type and execute. Run the program then answer the questions afterwards.
Decision
A Process A
Y
N
Decision
B Process B
Y
N
Decision
C Process C
Y
N
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 2/6
2
Example 1.1
Given the code above, what is the output if the User enters…1. …0?2. …2?3. …5?4. …6?Change the condition (x == 2) to (x == 1) like Example 1.2
Example 1.2
5. What is the output if the User enters 1?6. How many statements did it output? Why?7. What is the statement that I need to insert if I wanted to output“I’m hot!” when the User enters 4?8. Where should I insert this statement?
Remove the last statement of the If-else-if Ladder and return the
condition to (x == 2) like Example 1.3
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 3/6
3
Example 1.3
9. What is the output if the User enters 5? Why?10. What is the purpose of the last “else” statement which we
removed?11. Can an If-else-if Ladder use 2 control variables? Yes or No only.
Add another variable and change the program like Example 1.4
Example 1.4
12. What is the output if the User enters 0 then 2?13. How many statements did it output and why?
What is the output if the User enters…14. …3 then 3?
15. …1 then 4?
16. …2 then 0?17. When the If-else-if Ladder has two control variables, is it a Multi-
way Selection? Why?
That’s it for the If-elise-if Ladder. Let’s try the Switch statement
instead
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 4/6
4
Example 2.1
What is the output if the User enters…18. …0?19. …2?
20. …4?Change “case 0” to “case 1” like the Example 2.2
Example 2.2
21. What’s the difference between Example 2.1 and 2.2 aside fromchanging the 0 to 1?
Return the case to 0 and remove the break statements like Example
2.3
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 5/6
5
Example 2.3
What is the output if the User enters…
22. …0?23. …2?24. …4?25. What is the purpose of the break statement?
Return the break statement and remove the default case like
Example 2.4
Example 2.4
26. What is the output if the User enters 5? Why?
Return the default statement, followed by a break statement, butput it before the first case like Example 2.5
8/8/2019 17 - Muilti-Way Ex
http://slidepdf.com/reader/full/17-muilti-way-ex 6/6
6
Example 2.5
27. What is the output if the User enters anything? Why?
Put the default statement back at the end and remove the
statements inside case 0 and case 1 like Example 2.6
Example 2.6
What is the output if the User enters…28. …0?
29. …1?30. Why does it do that?
You’re done! Yehey!