Home > Back-end >  A switch statement
A switch statement

Time:10-13

#include
Int main ()
{
Int I, n=3, k=0;
for(i=1; i<=n; I++)
{
The switch (I)
{
Case 1: k +=1;
Case 2: k +=2;
Default: k +=3;
}
Printf (" % d ", k);
}
return 0;
}
This code didn't quite understand after cycle are three output value? The switch is in how to calculate?

CodePudding user response:

First we see I=1, after entering cycle case 1,=1 this statement is executed, k +, and then perform k +=2, and then perform k +=3, the final output k
Let I=2 into the circulation, directly to the case 2, the execution of k +=2, then perform k +=3, the output of k
Finally I=3 into the circulation, the direct execution of k +=3, the output of k
Will perform has nothing to do with the case what content because you didn't add behind each case break;

CodePudding user response:

Is because didn't break so I don't understand
If not break, you will get the rest of the executive

CodePudding user response:

Each loop output a value of
Cycle for the first time I==1, enter the case1, calculate k=0 + 1=1, because the case did not break, continue to enter the case2, calculating k=1 + 2=3, because there is no case break, continue to enter the default, calculating k=3 + 3=6, then execute the printing, 6
The second cycle, I==2, case1 unsatisfied, skip, enter the case2, calculating k=6 + 2=8, because the case did not break, continue to enter the default, calculating k=8 + 3=11, then execute the print, 11
The rest of the cycle so on,

CodePudding user response:

Forget it, just left the last cycle, direct also wrote
Each loop output a value of
Cycle for the first time I==1, enter the case1, calculate k=0 + 1=1, because the case did not break, continue to enter the case2, calculating k=1 + 2=3, because there is no case break, continue to enter the default, calculating k=3 + 3=6, then execute the printing, 6
The second cycle, I==2, case1 unsatisfied, skip, enter the case2, calculating k=6 + 2=8, because the case did not break, continue to enter the default, calculating k=8 + 3=11, then execute the print, 11
The third cycle, I==3, case1, case2 unsatisfied, enter the default, calculating k=11 + 3=14, then execute the print, 14

CodePudding user response:

Could you tell me how to judge the case which is whether to meet?

CodePudding user response:

reference 1st floor DYf3244 response:
first we see I=1, enter the cycle after the case 1,=1 this statement is executed, k +, and then execute k +=2, and then perform k +=3, the final output k
Let I=2 into the circulation, directly to the case 2, the execution of k +=2, then perform k +=3, the output of k
Finally I=3 into the circulation, the direct execution of k +=3, the output of k
Will perform has nothing to do with the case what content because you didn't add behind each case break;
why directly to the case 2, the second time for the third time will directly to the default?

CodePudding user response:

The
reference 4 floor qybao reply:
calculate, just left the last cycle, direct also wrote
Each loop output a value of
Cycle for the first time I==1, enter the case1, calculate k=0 + 1=1, because the case did not break, continue to enter the case2, calculating k=1 + 2=3, because there is no case break, continue to enter the default, calculating k=3 + 3=6, then execute the printing, 6
The second cycle, I==2, case1 unsatisfied, skip, enter the case2, calculating k=6 + 2=8, because the case did not break, continue to enter the default, calculating k=8 + 3=11, then execute the print, 11
The third cycle, I==3, case1, case2 unsatisfied, enter the default, calculating k=11 + 3=14, then execute the print, 14
could you tell me how to judge the case this is whether to meet?

CodePudding user response:

Swith (I) is to determine what is I will jump to the corresponding case to!
Jump into the case such as the I is 1, 1, and I will jump to the case 2, and so on, but if the case without break, will continue to carry out a case, such as the I is 1, after performing the case 1, because there is no break, will continue to implement the case 2, and so on, if you want I only perform case 1 is 1, so will break at the end of the case 1, the
Case 1: k +=1; break;

CodePudding user response:

refer to 6th floor weixin_46457534 response:
Quote: refer to 1st floor DYf3244 response:
first we see I=1, enter the cycle after the case 1,=1 this statement is executed, k +, and then perform k +=2, and then perform k +=3, the final output k
Let I=2 into the circulation, directly to the case 2, the execution of k +=2, then perform k +=3, the output of k
Finally I=3 into the circulation, the direct execution of k +=3, the output of k
Will perform has nothing to do with the case what content because you didn't add behind each case break;
why directly to the case 2, the second time for the third time will directly to the default?

Because the case is equivalent to the if, in case 1 part I==2, is not equal to, so just skip,

CodePudding user response:

There is no break in the switch, when I=1, jump to case 1, since there is no break, so the case 2, case 3 and default will perform, when I=2, jump to the case 2, since there is no break, so the case 3 and default implementation; When I=3, an so on
  • Related