int c,i;
for (int i = 1; i < 3; ++i)
{
switch (i)
{
default: c+=i;
case 2: c++;break;
case 4: c+=2;break;
}
}
printf("%d\n", c);
Why is this code equal to 3? Isn't this the case where default: c =i; is used when i is 1 for the first time, and case 2: c ;break; is used when i is 2 for the second time? Then it ends, and c=2 is output in the end? Why is it 3?
女神的闺蜜爱上我2017-07-06 10:37:10
First of all, let’s clarify some points to note in switch:
switch
statement body consists of a series of case
tags and an optional default
tag. case
Two constant expressions in a statement cannot evaluate to the same value. default
tag can only appear once. Marking statements is not a grammatical requirement, but if they are not present, the switch
statements are meaningless. The default statement (i.e. the default
tag) does not need to appear at the end; it can appear anywhere in the body of the switch statement. case
or default
tags can only be displayed within switch
statements.
Excerpted from: Microsoft Visual Studio 2015 c++ Switch statement official documentation
The above-mentioned case
and default
themselves are labels, which tell the compiler to start executing backwards when this label is satisfied, and then it will not judge the correctness of other labels until the break
statement or switch
statement The scope ends.
STEP 1 : When i=1
, since i!=2&&i!=4
, execution starts after the default
label. At this time, the statement c+=i;
(we now assume that compilation The compiler will help you initialize c to 0. You must know that not all compilers are so friendly) The value of c after execution is 1;
STEP 2: Based on the above 1 and 2, it can be seen that since no break is encountered at this time The
statement has not reached the end of the scope of the switch
statement (because this default
statement is placed first), so it then executes the statements after case 2
backwards (the compiler is no longer full at this time) The case
tag is not satisfied) At this time, the statement c++
is executed; after execution, the value of c is 2; when the break
statement is encountered, the switch
statement is jumped out.
STEP 3: When i=2
, since i satisfies case 2
, execution starts directly from the statement after the case 2
note. At this time, the statement c++
is executed. After execution, c The value is 3. When the break
statement is encountered, the switch
statement will be jumped out.
STEP 4: When i=3, jump out of the for
loop and output c=3;
(The above process is the conclusion drawn by me using Visual Studio 2015 single-step debugging and combined with the data)
三叔2017-07-06 10:37:10
When i=1, it enters default: c=1 and there is no break, so continue to match case 2 and get c=2 break
When i=2, it matches case 2 first and enters case 2: c=3 break
When i=3 The cycle does not hold.
The output c is 3
三叔2017-07-06 10:37:10
var c = 0,i;
for (var i = 1; i < 3; ++i)
{
switch (i) {
default: c+=i;console.log('defalult:'+c);
case 2: c++;console.log('case2:'+c);break;
case 4: c+=2;console.log('case4:'+c);break;
}
console.log(i, c);
}
console.log('last:'+c);