Update index.md (#36349)
Removed some statements talking about the optimization potential of goto (which is mentioned but never specified), and I gave a possible use-case for goto followed by some constructs that can replace it, and ended on why goto is discouraged in C++ Added information about the memory behaviour of labels, the syntax to create a label, and where a label can actually be placed (within the same function).
This commit is contained in:
committed by
Randell Dawson
parent
04790915a8
commit
53470d2fd8
@ -4,8 +4,16 @@ title: goto
|
|||||||
|
|
||||||
# Intro to the use of goto and labels
|
# Intro to the use of goto and labels
|
||||||
|
|
||||||
goto is one of the most powerful pieces of logic in C/C++. Crazy amounts of optimization can be achieved using goto, provided it is used properly. **It is, however, discouraged for use in C++, since better ways of programming exist, and it [leads to spaghetti code](https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto#3517746)**
|
`goto label` goes to the mentioned occurence of the `label`, which can be either before or after the `goto` statement, as long as the `label` is in the same function as the `goto` statement.
|
||||||
It does exactly what it is named as. It goes to the mentioned occurence of the next label, wherever may it be.
|
|
||||||
|
If a `goto` causes program execution to exit some scope where a variable is defined, then the variable will be destroyed. If multiple of these variables exist, then they will be destroyed in opposite order of their construction.
|
||||||
|
|
||||||
|
See https://en.cppreference.com/w/cpp/language/goto for more information.
|
||||||
|
|
||||||
|
A common use of `goto` is to break out of a multiply-nested loop following some condition. However, there are several C++ language constructs that can be used to avoid this use case, including early `returns`, refactoring into different functions, and local variables in the loop.
|
||||||
|
|
||||||
|
**The use of goto is discouraged in C++, since it encourages poor design and creates code that is hard to debug and trace through. https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto#3517746)**
|
||||||
|
|
||||||
|
|
||||||
# Terminology
|
# Terminology
|
||||||
|
|
||||||
@ -14,20 +22,17 @@ It does exactly what it is named as. It goes to the mentioned occurence of the n
|
|||||||
|
|
||||||
# Syntax
|
# Syntax
|
||||||
|
|
||||||
```C++
|
```cpp
|
||||||
goto label_name;
|
goto labelName; //This takes the program flow to the next appearance of label.
|
||||||
//This takes the program flow to the next appearance of label_name.
|
labelName: //to create a label name, write the name followed by a colon
|
||||||
```
|
|
||||||
Labels are defined as a name, followed by a colon (**:**)
|
|
||||||
```C++
|
|
||||||
label_name:
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Scope
|
# Scope
|
||||||
|
|
||||||
`goto` can only jump to a label in the **same scope** (set of braces - {}).
|
`goto` can only jump to a label in the **same scope** (set of braces - {}).
|
||||||
|
|
||||||
### Example:
|
### Example:
|
||||||
```C++
|
```cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main(){
|
int main(){
|
||||||
goto x:
|
goto x:
|
||||||
@ -43,7 +48,7 @@ int someFunction(){
|
|||||||
|
|
||||||
`goto` is something that transcends all loops. To be clearer on this point, here is an example.
|
`goto` is something that transcends all loops. To be clearer on this point, here is an example.
|
||||||
|
|
||||||
```C++
|
```cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
@ -59,12 +64,12 @@ label:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
[Try the code here!](https://wandbox.org/permlink/2zm4f4WMR7ybvJlQ)
|
|
||||||
|
|
||||||
Even though the above code works, a **much** better option is to structure your code such that `goto` is not needed for the program flow. For this reason, many modern programming languages (like java, javascript, python, etc.) do support `goto`. Instead, control statements like `break` and `continue` are used.
|
Even though the above code works, a **much** better option is to structure your code such that `goto` is not needed for the program flow. For this reason, many modern programming languages (like java, javascript, python, etc.) do support `goto`. Instead, control statements like `break` and `continue` are used.
|
||||||
|
|
||||||
The above example can be rewritten using `break` as:
|
The above example can be rewritten using `break` as:
|
||||||
```C++
|
|
||||||
|
```cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
@ -78,7 +83,6 @@ int main(){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
[Try the code here!](https://wandbox.org/permlink/faRaw7paaRwWGkln)
|
|
||||||
|
|
||||||
**However, care must be taken to use goto very carefully**, especially in the early days of coding as it can lead to crazy issues, if not understood well enough. `goto` violates the standard flow of the program, and as C++ is an object oriented language, goto should **NEVER EVER, EVER** be used in a normal program, under **ANY CIRCUMSTANCES**. The same effect can usually be replicated by using functions or loops, with the resulting code being easier to read as well as maintain.
|
**However, care must be taken to use goto very carefully**, especially in the early days of coding as it can lead to crazy issues, if not understood well enough. `goto` violates the standard flow of the program, and as C++ is an object oriented language, goto should **NEVER EVER, EVER** be used in a normal program, under **ANY CIRCUMSTANCES**. The same effect can usually be replicated by using functions or loops, with the resulting code being easier to read as well as maintain.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user