简体   繁体   中英

C - "if" statement will never be executed [-Wswitch-unreachable] in a switch statement

So i am trying to make an switch statement that checks if the user has not already entered that switch statement before and I am trying to do this with an if statement inside the switch, but it always ignores the if statement and always just completes the case which is inside it, disregarding the other arguments, but what is also interesting is that it changes a to bijis, but doesnt do the actual condition check:

char bijis = '*';
char izv;
printf("\nChoose the next station ");  
scanf("%d",&izv);  

switch(izv)  {  
if (a != bijis) {
    case 'a': case 'A':  
    a = bijis;
    jaut_a();
    break; 
} 
    if (b != bijis) {
        case 'b': case 'B':
        b = bijis;       
        jaut_b();  
        break;  
}
     if (c != bijis) {
        case 'c': case 'C':
        c = bijis;       
        jaut_c();  
        break;  
}
     if (d != bijis) {
        case 'd': case 'D':
        d = bijis;       
        jaut_d();  
        break;  
}
     if (e != bijis) {
        case 'e': case 'E':
        e = bijis;       
        jaut_e();  
        break;  
}
     if (k1 != bijis) {
        case '1':
        k1 = bijis;       
        jaut_k1();  
        break;  
}
     if (k2 != bijis) {
        case '2':
        k2 = bijis;       
        jaut_k2();  
        break;  
}
     if (k3 != bijis) {
        case '3':
        k3 = bijis;       
        jaut_k3();  
        break;  
}
     if (k4 != bijis) {
        case '4': 
        k4 = bijis;       
        jaut_k4();  
        break;  
}
     if (k5 != bijis) {
        case '5': 
        k5 = bijis;       
        jaut_k5();  
        break;  
}
     if (k6 != bijis) {
        case '6': 
        k6 = bijis;       
        jaut_k6();  
        break;  
}
     if (k7 != bijis) {
        case '7':
        k7 = bijis;       
        jaut_k7();  
        break;  
}
     if (k8 != bijis) {
        case '8' :
        k8 = bijis;       
        jaut_k8();  
        break;  
}
     if (k9 != bijis) {
        case '9' :
        k9 = bijis;       
        jaut_k9();  
        break;  
}
          default:  
          clearscr();
          printf("Kļūdaina ievade, šajā lauciņā jau esi bijis! Ieraksti jebkuru burtu vai skaitli,kas rādīts tabulā.\n (Zvaigznītes parāda vietas kur jau esi bijis.");
          sleep(3.5);
          karte_plans();
          break;  
    } ;

Your case labels are inside of the if blocks. The switch jumps directly to the relevant label, so the condition is jumped over.

Put the condition after the label.

switch(izv)  {  
case 'a': case 'A':  
    if (a != bijis) {
        a = bijis;
        jaut_a();
    } 
    break; 
case 'b': case 'B':
    if (b != bijis) {
        b = bijis;       
        jaut_b();  
    }
    break;  
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM