In: Computer Science
If a switch statement contains no break statements at all,
a. a syntax error will occur
b. each of the case clauses will be executed every time the switch
statement is encountered
c. this is equivalent to having the switch statement always take
the default clause, if one is present
d. this is not a syntax error but nothing within the switch
statement will ever be executed
e. None of these
Which one of these options?
If a switch statement does not contain any break statements, it is syntactically correct and each of the case clauses will be executed every time the switch statement is encountered and also the default statement along with it. Therefore, option b) is the most appropriate choice.
Option c) is not the correct choice because this is not equivalent to having the switch statement always take the default clause, if present. Default clause will be taken but along with the relevant cases as well.
Example :
#include <iostream>
using namespace std;
int main() {
// your code goes here
int num = 2;
switch(num)
{
case 1 : cout << num <<
endl;
case 2 : cout << num <<
endl;
default: cout << num <<
endl;
}
return 0;
}
The given code will print :
2 // This is for case 2
2 // This is for default case
Hope this resolves your doubt. Please give an upvote if you liked my solution.Thank you :)