Explanation of pros and cons of PHP goto statement

Source: Internet
Author: User

You know, a child with a goto and a call.

Then in the group to knock the code, the kitten with do ... Loop loop, the result is a kid in the group asking do ... What is the loop loop? -_-||| As a result, a group of people said they seldom use do ... Loop loop, most of them like to use Goto, I found this problem, not to say not to use Goto, but less use goto!!!

The crap is over, start the chase.

Explain the difference between Goto and call first for beginners

Goto is used to jump the code, that is, when the code executes to Goto, jumps to the position specified by REM and executes downward, and it does not return to the next line of the Goto statement when it finishes executing.

Call is also used to jump the code, but more accurately, calls is used to invoke subroutines! What is a call? That is, when you execute to call, the specified subroutine (sub) is placed in the code, and when the subroutine executes, it goes back to the next bar of the call statement and continues down execution.

If it's hard to understand, then use the code from the classmate you just started to explain:

This is a goto statement

1. a=152.      b=103.          If a-b < 154. Goto sub1//jumps to rem sub1 and does not jump back!!!              5. If a-B >-156.          Goto Sub27.      End If8.       End If9.   The Rem sub211.    MsgBox "A-b greater than-15" 12.   Rem sub114.
   MsgBox "A-B less than 15"//script to the bottom, execution end!!! 
1. a=152.      b=103.          If a-b < 154.          Call sub1//calls the Sub1 subroutine, and when the subroutine execution is finished, return to here and continue to execute 5.              If a-B >-156.          Call Sub27.      End If8.       End If9.       Ten. Sub sub2 () 11.   MsgBox "A-b greater than-15" 12.    End Sub13.       Sub sub1 () 15.   MsgBox "A-B less than 15" 16. End Sub 


OK, the two differences are clear, now talk about this topic, the pros and cons of goto statement!!!

As I said above, I advocate Goto to use less, absolutely can not abuse, among them, the main reason is that goto statement destroys the program structure, so that the program readability is poor, you use a, two may not have a small impact, but if you use more than 10 goto it?

The following paragraph from the Baidu Encyclopedia (if you feel too much, then you just see "goto statement Results" that paragraph)

Problem Origin:

After the middle of the 60 's, the computer hardware technology is progressing, the storage capacity, computing speed and reliability of the calculation are obviously improved, and the cost of production hardware is decreasing continuously. The fall in computer prices has created excellent conditions for its wide application. In this situation, it is urgent that computer software can adapt to it. As a result, some requirements for developing large software systems have been raised. However, the development of software technology has been unable to meet the needs of the situation, in the development of large-scale software has a high degree of complexity, long development cycle, the correctness of the three difficult to guarantee. The problems encountered can not find a solution, causing problems piled up, forming a situation that people are difficult to control, there is a so-called "software crisis." In order to overcome this crisis, it is necessary to study the design method, the correctness of the program and the reliability of the software. On the other hand, it is also necessary to study the methods of software compiling, testing, maintenance and management, thus producing the programming methodology.

  A goto statement is a harmful point of view:

1968, E. W. The argument that "goto statement is harmful", the author puts forward the challenge to the traditional method of programming, which arouses the general attention to the discussion of programming method.

  The argument to goto statement:

In the late 60 and early 70, there was a heated debate about the use of goto statements. Advocates of removing a goto statement from a high-level programming language believe that the goto statement is a harmful statement that has the greatest impact on the program structure, and their main reason is that the goto statement makes the program's static structure and dynamic structure inconsistent, making the program difficult to understand and difficult to find. After you remove the goto statement, you can directly reflect the program's process from the program structure. In this way, not only the program structure is clear, easy to understand, easy to check the wrong, but also conducive to the correctness of the program proof.

Opponents argue that the goto statement is more flexible to use and that some situations can improve the efficiency of the program. If you delete the goto statement completely, some cases will make the program overly complex and add some unnecessary computational capacity.

  Workaround for Goto statement:

1974, D. E. Knus a full and impartial commentary on the Goto statement controversy, with the basic idea of using the goto statement without restriction , especially with a goto statement that jumps back. Makes the program structure difficult to understand, in which case you should try to avoid using GOTO statements. In other cases, however, 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 would argue for the deletion of goto statements; In other cases, the introduction of goto statements is advocated." "From then on, the 10-year-long controversy has been appeased.

Later, G. Gacoppigny and C. Bohm theoretically proved that any program can be expressed in order, branching, and repeating structures. This conclusion shows that the removal of the Goto statement from the Advanced programming language does not affect the programming ability of the high-level program language, and the structure of the written program is clearer. [/hide]

  Goto The result of the statement:

Goto statements are retained in high-level programming languages such as C + +, but are not recommended or used sparingly. In some newer high-level programming languages, such as Java does not provide a goto statement, although it specifies goto as the keyword, but does not support its use, making the program concise and easy to read; The Goto statement has the advantage of being able to guarantee that the program has a unique exit, even though the subsequent C # still supports the goto statement. Avoid overly large if nesting.

If you have a careful look at the above paragraph of the classmate, I believe you can understand the advantages and disadvantages of the goto statement it?

Well, let's just explain it in code:

Examples of abusive goto statements

1. Rem A2.          If False3.      Goto B4.       End If5.      6. Rem C7.          If True8.      Goto A9.       ElseIf False10.   Goto B11.       Else 12.   Goto C13.    End If14.   . Rem B16. Is this code readable, and can you draw a flowchart? And Goto can be completely replaced by other statements.      
1.          Do2.              Do3.                  Do4.                      If True5.                  Goto stop//use Goto to jump out of deep nesting 6.              End If7.          LOOP8.      LOOP9.    Loop10. Rem Stop 


explained here, the idea of the kitten is to suggest that everyone If you can not goto do not Goto, as much as possible to use call, if you want to jump out of the loop, each cycle has a corresponding jump statement, such as Exit Do,exit for,exit sub,exit Function
In addition, when we need to use the loop, we recommend using do ... Loop and for ... Next, while the while loop compares the bypass, you can replace him with do and for

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.