I Can't Stop Using Goto Statments-Help!

Hey,
I've only been programming for a short time and though I realize it is bad,its hard to stop using goto statements. Im playing around with a simple text-based game, just pressing a number and going that way or whatever and have finally realized why they are so bad to be using. I need help on an alternative way of using goto statements. Please help me.


P.S.
I don't need a lecture on why I shouldn't use goto statements. I've read plenty before.
Last edited on
Last edited on
Some people use gotos instead of loops. To avoid that you can do:
1
2
3
4
5
6
7
int main()
{
  start:
  //Stuff here
  if (condition) 
    goto start;
}


This can become:

1
2
3
4
5
6
7
8
int main()
{
  bool condition = true;
  while(condition)
  {
    //Do your stuff here.
  }
}


Some people use goto to get out of a loop early:
1
2
3
4
5
6
while(true)
{
  if (getOut) goto NextStep;
  //Stuff here
}
NextStep:

can become:
1
2
3
4
5
while(true)
{
  if (getOut) break;
  //Stuff here
}


Some people use it to exit a program early:
1
2
3
4
5
6
7
int main()
{
  if (condition) goto end;
  //Stuff here
  end:
  return 0;
}

But you can do this instead:
1
2
3
4
5
6
int main()
{
  if (condition) return 0;
  //Stuff here;
  return 0;
}
Last edited on
If programmers did Jerry Springer, the title of this thread would be on there.
Topic archived. No new replies allowed.