#include<stdio.h> int main() { int a=1,i; for(i=0;i<3;i++) switch((++a)-1) { case 0:printf("\nzero"); case 1:printf("%d.one",i+1); case 2:if(i%2==0) printf("\n\t%d.Two",i+1); else printf("\n%d.Two",i+1); default:printf("\t%d.step",i+1); } }su salida es:
1.one 1.Two 1.step 2.Two 2.step 3.stepNo puedo entender por qué hay seis salidas donde deberían ser tres, supongo.
Porque sin break; declaración, cada caso continuará en el siguiente.
switch((++a)-1) { case 0:printf("\nzero"); // Without a break;, the next line gets executed as well! case 1:printf("%d.one",i+1); // Execution continues here, printing "%d.one", even though we are in case 0 case 2:if(i%2==0) { printf("\n\t%d.Two",i+1); } else { printf("\n%d.Two",i+1); } // The default will get run as well, for lack of a "break;" default:printf("\t%d.step",i+1); }Esto debe escribirse correctamente como:
switch((++a)-1) { case 0: printf("\nzero"); break; case 1: printf("%d.one",i+1); break; case 2: if(i%2==0) { printf("\n\t%d.Two",i+1); } else { printf("\n%d.Two",i+1); } break; default: printf("\t%d.step",i+1); break; }