Java syntax (7): Brake and Label

Source: Internet
Author: User
Java syntax (7): Brake and Label-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1: break

The break statement provides a convenient way to jump out of the loop.



Boolean test = true;
Int I = 0;
While (test)
{
I ++;
If (I> = 10) break;
}

When executing this program, although the while condition expression is always true, the full loop runs only 10 times.

2: label

The label provides a method for controlling loops that cannot be implemented by a simple break statement. When a break is encountered in a loop statement, it is terminated regardless of other control variables. But what happens when you want to exit the loop nested in several layers of loops? A normal break exits only one loop. You can use a label to indicate which statement you want to exit.

Char;
Outer: // this is the label for the outer loop
For (int I = 0; I <10; I ++)
{
For (int j = 0; j <10; j ++)
{
A = (char) System. in. read ();
If (a = 'B ')
Break outer;
If (a = 'C ')
Continue outer;
}
}

In this example, the cycle receives 100 input characters from the keyboard. When "B" is entered, the break outer statement ends the two loop. Pay attention to the continue outer statement, it tells the computer to exit the current loop and continue executing the outer loop.

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.