First, examine the following code: int numValue = 10; int answer = 0; switch(num
ID: 3686178 • Letter: F
Question
First, examine the following code:
int numValue = 10;
int answer = 0;
switch(numValue)
{case 5: answer += 5;
case 10: answer += 10;
case 15: answer += 15;
break;
case 20: answer += 20;
case 25: answer += 25;
default: answer = 0;
break;
}
cout << "Answer: " << answer << endl;
1. What is the value of answer if the value of numValue is 10?
2. What is the value of answer if the value of numValue is 20?
3. What is the value of answer if the value of numValue is 5?
4. What is the value of answer if the value of numValue is 17?
5. Is the break statement in the default case needed? Explain.
Explanation / Answer
1) 25
2) 0
3)30
4)0
5) break statement is not needed in default case. Because default case is executed only when none of the other cases are true. The purpose of break is to stop from running through other cases, but in default case we already know that all other cases are not true so there is no needed in specifying a break statement.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.