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>
usingnamespace std;
int main(){
int x = 10;
randomName:
if(x <= 10)goto randomName;{
cout << "hi";
}
}
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.
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.