-4

In the below code the if condition is overridden by the goto statement and in the subsequent one, if condition in switch is overridden too. Please explain why goto and switch is doing this and is there a way to put condition on goto and inside switch as well.

int main()
{
    int x=3;
    goto LABEL;

    if(x < 0) {
        LABEL: printf("Label executed");
    }

    printf("\nEND MAIN");
    return 0; 
}

OUTPUT:

Label executed
END MAIN

int main()
{  
    int x = 2, y = -5;

    switch(x)
    {   if( y > 0)
        {   case 1:
                printf("case 1");
                break;
            case 2:
                printf("\n case 2");
                break;        
        } 
        case 3:
            printf("\n case 3");
            break;
        default :
            printf("\n Exit switch");  
     }
}

OUTPUT:

case 2
SiggiSv
  • 1,219
  • 1
  • 10
  • 20
  • 2
    Please read a [suggested C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn about basic concepts like goto – StoryTeller - Unslander Monica Jun 12 '17 at 12:53
  • 2
    Maybe you want to leave the 1960ies/70ies and learn about structured programming. Spaghetti are fine on a plate with a good sauce, but not in programming. C provides loop statements to avoid writing such awful code. – too honest for this site Jun 12 '17 at 12:56
  • 1
    Basically this is a misuse of goto. Whilst C has rules for resolving the issues, they don't matter to anyone except a compiler writer or an obfuscated C competition entrant. Don't jump into if blocks or put if blocks round switch cases. – Malcolm McLean Jun 12 '17 at 12:56
  • 1
    It all boils down to: please don't grab a random piece of code from external sources and ask stackoverflow to explain it for you. Instead, learn from sources where the examples are delivered with an explanation so you can understand them for yourself. Once you learned the basics, you may ask specific quiestions here - but please one at a time and not multiple questions at once. – grek40 Jun 12 '17 at 13:00
  • @Olaf: Flippin' cheek!! I didn't use `goto` in the 60s and 70s either. That was for COBOL programmers. – cdarke Jun 12 '17 at 13:00
  • Ok got it. Thanks everyone! – Vishal Rana Jun 12 '17 at 13:03
  • I apologize if you consider this as simple question – Vishal Rana Jun 12 '17 at 13:04
  • 1
    @cdarke: Yes, and I had to use it in the 80ies, too in Apple BASIC and on the VIC64, but mostly in my assembly code (in the form of jump/branch instructions). But I think you got my point :-). – too honest for this site Jun 12 '17 at 13:13
  • @Olaf - only kidding. – cdarke Jun 12 '17 at 13:15
  • I haven't seen `goto` for ages – MrLeeh Jun 12 '17 at 13:38
  • Thanks @SiggiSv for ur editing – Vishal Rana Jun 12 '17 at 17:19

1 Answers1

2

goto jumps to this line:

LABEL: printf("Label executed");

ignoring the if statement and its condition, which explains the output.


switch/case is just a fancy goto, thus switch(x), with x = 2, will go to case 2, ignoring the if statement, which explains the output.


PS: goto can lead to spaghetti code, thus is considered to be the black sheep of programming. It is suggested not to use it, unless you really really must to. Read more in What is wrong with using goto?

gsamaras
  • 71,951
  • 46
  • 188
  • 305