#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?