goto statement

Hi guys I just want to know what is the goto statement and when and why is it used,I made a small (useless) program with the goto statement and my program does nothing and also does not seem to end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  
  #include <iostream>

using namespace std;


int main(){

    int x = 10;
    randomName:

    if(x <= 10)goto randomName;{

        cout << "hi";
    }
}
Don't use it.

It makes the program flow jump to the label mentioned.

So here, the code on line 12, goto randomName;, makes the flow of the program jump to line 10 (because that's where the label randomName: is) and then the program goes on to line 12 and then goto randomName; makes the flow of the program jump to line 10 and then the program goes on to line 12 and then the program goes on to line 12 and then goto randomName; makes the flow of the program jump to line 10 and then the program goes on to line 12 and then the program goes on to line 12 and then goto randomName; makes the flow of the program jump to line 10 and then the program goes on to line 12 and then the program goes on to line 12 and then goto randomName; makes the flow of the program jump to line 10 and then the program goes on to line 12 and then the program goes on to line 12 and then goto randomName; makes the flow of the program jump to line 10 and then the program goes on to line 12 ...

Don't use it. There are much better and clearer ways to controlling program flow.
yeah it seems like a simple for loop or while loop would be a lot simpler,why do people even use it,it seems like it should be deprecated
It is often used by beginners who do not know better, or have been taught by people who do not no better. You should opt to control program flow with "normal" control flow statements instead.

There are rare cases where it has its uses, but in general it should be avoided.
Even if goto will be prohibited, it can be anyway available by this way:
1
2
3
4
5
  mrp:printf("rrr\n");
  _asm
  {
    jmp mrp
  }
:)
Topic archived. No new replies allowed.