-1

here I used "goto" and when I call one goto like name1 with if else it all the values of gotos. like all the name1 name2 name3 name4. Help!!!

#include<stdio.h>
int main(){
    int num;
    printf("Name number\n");
    scanf("%d",&num);

    if(num==1)
    goto name1;
    else
    goto name2;
    name1:
      printf("M");
    name2:
      printf("A");
    name3:
      printf("I");
    name4:
      printf("Y");

    return 0;
}

if I give the value 1 or 2 it shows MAIY

Neo
  • 27
  • 3
  • 1
    Can't reproduce! When I enter 2 I see (as expected) "AIY". – Adrian Mole Apr 30 '20 at 14:17
  • 1
    Cannot reproduce. When I enter `2` it outputs `AIY`. You might like to read [Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?](https://stackoverflow.com/questions/24451/is-it-ever-advantageous-to-use-goto-in-a-language-that-supports-loops-and-func) – Weather Vane Apr 30 '20 at 14:18
  • Here´s another "Can´t reproduce"-r. If I input `2` I do also get `AIY` instead of `MAIY`. How do you compile? – RobertS supports Monica Cellio Apr 30 '20 at 14:21
  • I think you learned the lesson: avoid gotos. It is an antipattern and makes your code brittle. – user1708042 Apr 30 '20 at 14:53
  • Yeah - it's a waste of effort to learn about goto and then unlearn it after. Best to skip both steps:) – Martin James Apr 30 '20 at 18:04

2 Answers2

2

"when I call one goto like name1 with if else it all the values of gotos. like all the name1 name2 name3 name4. Help!!!"

It seems that you misunderstanding the use of goto. A goto label doesn´t specify a certain group of statements (compound statement/block) or just a single statement to it like it is f.e. to a particular condition match to a if/elseif chain or a switch/case statement.

The goto name1 statement just let you jump to the position of the label name1. Nothing more, nothing less.

If you want to achieve what you described, use a switch like:

#include<stdio.h>

int main(){

    int num;
    printf("Name number\n");
    scanf("%d",&num);

    switch(num) {

    case 1:
      printf("M");
      break;

    case 2:
      printf("A");
      break;

    case 3:
      printf("I");
      break;

    case 4:
      printf("Y");
      break;

    default:
      break;

    }

    return 0;
}

Note that the use of goto as you did is commonly deprecated.

You should take a look at these posts:

Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?

GOTO still considered harmful?

1

If you want to use switch you can do as the answer of @Roberts. But if you still want to use goto for this case, you can add new label after each printf function to break out some statement you do not want to execute. For example:

#include<stdio.h>

int main(){
    int num;
    printf("Name number\n");
    scanf("%d",&num);

    if(num==1)
       goto name1;
    else
       goto name2;
    name1:
      printf("M");
      goto end_name;
    name2:
      printf("A");
      goto end_name;
    name3:
      printf("I");
      goto end_name;
    name4:
      printf("Y");
    end_name:
       printf("\nHello World ===> Do something else from here\n");
    return 0;
}

The output like:

Name number                                                                                                             
1                                                                                                                       
M                                                                                                                       
Hello World ===> Do something else from here
Hitokiri
  • 3,607
  • 1
  • 9
  • 29