Home > Article > Backend Development > Detailed explanation of the pros and cons of php goto statement
You can tell at a glance that he is a kid who can’t tell the difference between goto and call
Then when he was typing code in the group, the kitten used Do...Looploop, but a child in the group asked Do... What is a loop? -_-|||As a result, a group of people said that they rarely use Do...Loop loops, and most of them like to use Goto. I discovered this problem. It does not mean that Goto cannot be used, but that Goto is used less! ! !
The nonsense is over, let’s get down to business
First explain the difference between Goto and Call for novices
Goto is used to jump to the code, that is, when the code is executed to Goto , jump to the position specified by Rem and execute downward. It will not return to the next statement of the Goto statement when it is completed.
Call is also used to jump codes, but to be more precise, Call is used to call subroutines! What is a call? That is, when the Call is executed, the specified subroutine (Sub) is placed in the code. When the subroutine is executed, it will return to the next line of the Call statement and continue to execute downwards.
If it is difficult to understand, then use the code of the classmate mentioned at the beginning to explain:
This is the Goto statement |
This is the Call statement |
1. a=15 2. b=10 3. if a-b < 15 4. goto sub1//跳转至Rem sub1处,且不会再跳回来了!!! 5. if a-b > -15 6. goto sub2 7. end if 8. end if 9. 10. Rem sub2 11. MsgBox "a-b大于-15" 12. 13. Rem sub1 14. MsgBox "a-b小于15"//脚本到底部了,执行结束!!! |
1. a=15 2. b=10 3. if a-b < 15 4. call sub1//调用sub1子程序,当子程序执行结束后,返回到此处,继续向下执行 5. if a-b > -15 6. call sub2 7. end if 8. end if 9. 10. Sub sub2() 11. MsgBox "a-b大于-15" 12. End Sub 13. 14. Sub sub1() 15. MsgBox "a-b小于15" 16. End Sub |
OK, the two differences have been clearly explained. Now let’s talk about this topic, the pros and cons of the Goto statement! ! !
As I said above, I advocate that Goto should be used less and must not be abused. One of the main reasons is that the Goto statement destroys the program structure and makes the program less readable. If you use one, Two may not have much impact, but what if you use more than a dozen Gotos?
The following paragraph is taken from Baidu Encyclopedia (if you think it is too much, then you only need to read the "Results of the goto statement" section)
Origin of the problem:
After the mid-1960s, computer hardware technology has improved day by day. The storage capacity, computing speed and reliability of computing have increased significantly, and the cost of producing hardware has continued to decrease. The decline in computer prices has created excellent conditions for its widespread application. In this situation, there is an urgent need for computer software to adapt to it. Therefore, some requirements for developing large-scale software systems have been put forward. However, the progress of software technology has not been able to meet the needs of the development of the situation. In the development process of large-scale software, three major problems have arisen: high complexity, long development cycle, and difficulty in ensuring accuracy. There is no solution to the problems encountered, which causes problems to pile up and form a situation that is difficult for people to control. The so-called "software crisis" occurs. In order to overcome this crisis, on the one hand, it is necessary to conduct a series of research on issues such as programming methods, program correctness and software reliability; on the other hand, it is also necessary to conduct research on methods of software preparation, testing, maintenance and management. , thus producing programming methodology.
The view that goto statements are harmful:
In 1968, E.W. Dykstra first put forward the argument that "GOTO statements are harmful", pointing to the traditional Programming methods pose challenges that have drawn widespread attention to the discussion of programming methods.
Controversy over the goto statement:
In the late 1960s and early 1970s, the debate over the usage of the GOTO statement was fierce. People who advocate removing the GOTO statement from high-level programming languages believe that the GOTO statement is a harmful statement that has the greatest impact on the program structure. Their main reason is that the GOTO statement makes the static structure and dynamic structure of the program Inconsistency makes the program difficult to understand and troubleshoot. After removing the GOTO statement, the program running process can be reflected directly from the program structure. In this way, it not only makes the program structure clear, easy to understand and error-checking, but also helps to prove the correctness of the program.
Opponents believe that the GOTO statement is more flexible to use, and in some cases it can improve the efficiency of the program. If the GOTO statement is completely deleted, in some cases it will make the program too complex and increase unnecessary calculations.
About the solution to the goto statement:
In 1974, D.E. Knuth made a comprehensive and fair review of the GOTO statement controversy. His basic point is : Using the GOTO statement without restrictions, especially the GOTO statement that jumps back, will cause the program structure to change. It is difficult to understand. In this case, you should try to avoid using the GOTO statement. But in other cases, in order to improve the efficiency of the program without destroying the good structure of the program, it is necessary to use some GOTO statements in a controlled manner. In his words: "In some cases, I advocate deleting the GOTO statement; in other cases, I advocate the introduction of the GOTO statement." Since then, this 10-year-old debate has been settled.
Later, G. Giacoppini and C. Boehm theoretically proved that any program can be expressed using sequence, branching and repeating structures. This conclusion shows that removing the GOTO statement from a high-level programming language does not affect the programming ability of the high-level programming language, and the structure of the written program is clearer. [/hide]
gotoThe result of the statement:
The goto statement is retained in advanced programming languages such as C/C++, but it is recommended not to be used or to be used sparingly. In some updated high-level programming languages, such as Java, which does not provide goto statements, although it specifies goto as a keyword, it does not support its use, making the program concise and easy to read; nevertheless, later c# still supports goto For statements, one advantage of the goto statement is that it can ensure that the program has a unique exit and avoid too large if nesting.
如果有仔细看上面这段话的同学,相信能明白goto语句的利与弊了吧?
好吧,还是用代码来解释:
滥用goto语句的例子 |
合理利用goto语句 |
1. Rem A 2. If false 3. goto B 4. End If 5. 6. Rem C 7. if true 8. goto A 9. ElseIf false 10. goto B 11. else 12. goto C 13. End If 14. 15. Rem B 16. //这样的代码可读吗,你可以划出流程图吗。而且goto完全是可以用其他语句代替的。 |
1. Do 2. Do 3. Do 4. If true 5. Goto 停止//利用goto跳出深层嵌套 6. End If 7. Loop 8. Loop 9. Loop 10. 11. Rem 停止 |
讲解就到这里了,小猫的想法就是建议大家如果能不用goto就不用goto,尽量多使用Call,如果想跳出循环,每种循环都有对应的跳出语句,如exit do,exit for,exit sub,Exit Function
另外当大家需要用到循环时,建议使用Do...Loop和For...Next两者,而while循环比较绕口,完全可以用do和for代替他
The above is the detailed content of Detailed explanation of the pros and cons of php goto statement. For more information, please follow other related articles on the PHP Chinese website!