[Reprinted] several questions about Java continue and break

Source: Internet
Author: User
Tags domain name registration
Recently, jdk1.4.2 and eclipse3.0.1 are installed on the machine. Eclipse is really easy to use, especially the parameter prompt function.
There are still a lot of Java resources on the Internet. I read the first few chapters in the Chinese version of thinking in Java. Here are some of the issues and findings:
1. A problem with the For Loop
For (INT I = 1, j = I + 10; I <5; I ++, j = I * 2)
System. Out. println ("I =" + I + "J =" + J );

Running result:
I = 1 J = 11
I = 2 J = 4
I = 3 J = 6
I = 4 J = 8

After a few changes:
For (INT I = 1, j = I + 10; I <5; I ++ ){
J = I * 2;
System. Out. println ("I =" + I + "J =" + J );
}

The result is:
I = 1 J = 2
I = 2 J = 4
I = 3 J = 6
I = 4 J = 8

After asking for help on the internet, I found that the reason is: after the for loop executes the conditional test, it executes the program part first, and then it is just a step.

2. Use break and continue to add labels
In Java statements, the only place where labels can be placed is in front of the loop statement. In addition, there cannot be anything between the loop statement and the tag.
Label1:
Outer-iteration {
Inner-iteration {
//...
Break; // 1
//...
Continue; // 2
//...
Continue label1; // 3
//...
Break label1; // 4
}
}

Case 1 will interrupt the internal loop. 2. It will interrupt the current internal cycle and directly jump into the next cycle. Case 3 breaks the internal and external loops, jumps to label1, and starts the loop again from the external beginning. Case 4 jumps to label1.

The following is an example:

Public class labeledfor {
Static Test monitor = new test ();
Public static void main (string [] ARGs ){
Int I = 0;
Outer: // cant have statements here
For (; true;) {// Infinite Loop
Inner: // cant have statements here
For (; I <10; I ++ ){
System. Out. println ("I =" + I );
If (I = 2 ){
System. Out. println ("continue ");
Continue;
}
If (I = 3 ){
System. Out. println ("break ");
I ++; // otherwise I never
// Gets incremented.
Break;
}
If (I = 7 ){
System. Out. println ("Continue outer ");
I ++; // otherwise I never
// Gets incremented.
Continue outer;
}
If (I = 8 ){
System. Out. println ("break outer ");
Break outer;
}

For (int K = 0; k <5; k ++ ){
If (k = 3 ){
System. Out. println ("Continue inner ");
Continue inner;
}
}
}
}
}
}///:~

Result:
"I = 0 ",
"Continue inner ",
"I = 1 ",
"Continue inner ",
"I = 2 ",
"Continue ",
"I = 3 ",
"Break ",
"I = 4 ",
"Continue inner ",
"I = 5 ",
"Continue inner ",
"I = 6 ",
"Continue inner ",
"I = 7 ",
"Continue outer ",
"I = 8 ",
"Break outer"

Article: Western Digital-professional domain name registration and virtual hosting services
Http://www.west263.com
The above information is an integral part of the text. If you want to reprint this article, please keep the above information. Thank you!

Related Topics
  • Principle of inaccuracy in Java _ Java Authentication
  • Difference between the two strings "equals" and "=" in Java-JSP tutorial, Java skills and code
  • Call External commands in Java-JSP tutorial, Java skills and code
  • Comparison of four XML methods in Java-JSP tutorial, Java and XML
  • Java uses JMF to compile the camera program-JSP tutorial, Java skills and code
  • 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.