Switch Chassis Fall Scenario
So, I ran into a competitive question (asking the output) like this:
#include <stdio.h>
int main()
{
int i = 0;
for(i = 0; i < 20; i++)
{
switch(i)
{
case 0: i+=5;
case 1: i+=2;
case 5: i+=5;
default: i+= 4;
break;
}
printf("%d ", i);
}
return 0;
}
Output signal 16, 21
. Although I know how the switch works, I cannot explain how it works. Why is the default addition being added? Doesn't the K & RC book say that the default is executed if none of the cases match?
Thank.
source to share
The default case only bounces off the operator switch
if no other cases match. After one of them matches, the code is executed as if none of the operators case
existed, if it did not fall into break
. Thus, the case default
does not "jump" as you would expect.
K and R are a little unclear, the line you are linking to looks like this:
If the case matches the value of the expression, execution starts in that case. All case statements must be different. The default flagged case is executed if none of the other cases are satisfied
But that speaks to how the operator is being conducted switch
. Behavior on the next page:
Because cases serve as shortcuts, after the code for one case is complete, execution continues to the next unless you take explicit action to exit.
which does not depend on whether the case exists default
.
The C standard is clearer:
The switch statement causes the control to jump to, to, or from the statement that is the switch body, depending on the value of the control expression ... If the converted value matches the value of the extended control expression, control passes to the statement following the matching case label ... Otherwise, if there is a default label, control passes to the specified expression.
After control transitions, the case
and labels default
no longer matter.
source to share
The "body" of an operator switch
is one contiguous linear compound statement. Labels case
are just those labels that define the different entry points into this continuous statement. A label is default
also a label that does not differ from labels case
in this respect.
After you have entered the compound statement switch
through the label, execution continues in the usual way: sequentially until the end of the compound statement (at this stage case
/ default
labels are missing longer to play any role). If you want to prevent this from happening, you are responsible for jumping out of this tricky statement at the appropriate time. You can use any branch operator for this, keeping in mind that you also have break
(which is the most appropriate choice in most cases).
In other words, switch
it is not a highly structured branch selector as it might seem at first glance. switch
is just a slightly structured multipurpose goto
. All it does is a one-shot parameterized jump. Everything after this jump is your responsibility.
In the code example, you placed a single break
at the very end of the compound statement switch
. In this position, he actually achieved nothing, since the compound statement ends there.
source to share
In cases with a switch other than the standard, there is no "break".
If a switch statement is used without breaks, the code continues to run even after a matching match file is found.
See below the corrected code:
switch(i)
{
case 0: i+=5;
break;
case 1: i+=2;
break;
case 5: i+=5;
break;
default: i+= 4;
break;
}
source to share