State whether the following are valid switch statements. If not, explain why. As
ID: 3692124 • Letter: S
Question
State whether the following are valid switch statements. If not, explain why. Assume
that n and digit are int variables.
a) switch (n <= 2)
{
case 0:
cout << "Draw." << end1;
break;
case 1:
cout << "Win." << end1;
break;
case 2:
cout << "Lose." << end1;
break;
}
b) switch (digit / 4)
{
case 0,
case 1:
cout << "low." << end1;
break;
case 1,
case 2:
cout << "middle." << end1;
break;
case 3:
cout << "high." << end1;
}
c) switch (n % 6)
{
case 1:
case 2:
case 3:
case 4:
case 5:
cout << n;
break;
case 0:
cout << end1;
break;
}
d) switch (n % 10)
{
case 2:
case 4:
case 6:
case 8:
cout << "Even";
break;
case 1:
case 3:
case 5:
case 7:
cout << "Odd";
break;
}
Explanation / Answer
a) switch (n <= 2) is not allowed.
b)case 0, comma is not allowed. only colon is allowed.
c)switch statement is correct.
d)switch statement is correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.