The GOTO statement in Java!

Source: Internet
Author: User

The GOTO statement is a reserved word in Java and is not implemented. However, in other languages (C), Goto is a valid keyword.
Although the break and continue supported in Java can implement the GOTO statement function, I personally summarize that their usage is similar.

First, the label requirements in Java are very strict.
The label must be in front of a loop, which means that the loop name is outer (assuming the label name is outer), and there cannot be any statement after this line;
Break and continue are used in combination with loop statements. Therefore, the use of statement labels is also closely integrated with loops.
When statement labels are used, they can only be defined before the loop iteration statement. Otherwise, the compilation fails.
Therefore, the break and continue with a label can only jump to the loop body or select the body (IF ).

The (c) goto rules are flexible, and the labels in (c) can be before any legal statement.
Therefore, Goto can jump anywhere in a function (C language) (of course, it cannot violate the rational usage of the GOTO statement, for example, it cannot jump between nesting)

Therefore, my personal summary
The break and continue with labels are limited to jump in the loop body.
The labeled goto can be redirected in the entire method (C language function ).
Goto is more flexible than break and continue with labels, because it is too flexible.
The logic structure of the program becomes complex, the process is not clear enough, and the readability of the program is reduced.
So the Goto is retained in Java.

The GOTO statement is a reserved word in Java and is not implemented. However, in other languages (C), Goto is a valid keyword.
Although the break and continue supported in Java can implement the GOTO statement function, I personally summarize that their usage is similar.

First, the label requirements in Java are very strict.
The label must be in front of a loop, which means that the loop name is outer (assuming the label name is outer), and there cannot be any statement after this line;
Break and continue are used in combination with loop statements. Therefore, the use of statement labels is also closely integrated with loops.
When statement labels are used, they can only be defined before the loop iteration statement. Otherwise, the compilation fails.
Therefore, the break and continue with a label can only jump to the loop body or select the body (IF ).

The (c) goto rules are flexible, and the labels in (c) can be before any legal statement.
Therefore, Goto can jump anywhere in a function (C language) (of course, it cannot violate the rational usage of the GOTO statement, for example, it cannot jump between nesting)

Therefore, my personal summary
The break and continue with labels are limited to jump in the loop body.
The labeled goto can be redirected in the entire method (C language function ).
Goto is more flexible than break and continue with labels, because it is too flexible.
The logic structure of the program becomes complex, the process is not clear enough, and the readability of the program is reduced.
So the Goto is retained in Java.

Related Article

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.