Home > Back-end >  And while the switch
And while the switch

Time:10-30

 # include 
Int main (void)
{
int i=0;
While (i<3)
{
The switch (i++)
{
Case: 0 printf_s (" fat ");
Case 1: printf_s (" hat ");
Case 2: printf_s (" cat ");
Default: printf_s (" oh no!" );
}
Putchar (" \ n ");
}
return 0;
}

This loop to print out the three lines are respectively
Fat hat cat oh no!
Hat cat oh no!
The cat oh no!
Could you tell me why will so print

CodePudding user response:

My understanding is respectively to print 3 lines
Fat
Hat
cat

CodePudding user response:

Case: 0 printf_s (" fat "); break;
Case 1: printf_s (" hat "); break;
Case 2: printf_s (" cat "); break;
Default: printf_s (" oh no!" ); break;
You break

CodePudding user response:

Add break

CodePudding user response:

No break; Case statement last if there is a break in the same way as you want

CodePudding user response:

The switch of the building Lord lack of break; If in each case a break after it; Will output:
Fat
Hat
The cat
Try and break;

Without break; Order will be executed first starting from 0 ~ case 1 and case 2, the default, the second from case 1, case 1, case 2, the default and so on ~
  • Related