Execution order problem and break in Java for loop, continue usage

Source: Internet
Author: User
Tags continue execution
Problem | loop | Implementation recently installed the jdk1.4.2 on the machine, loaded the editor Eclipse3.0.1, after the feeling eclipse is really good, especially the parameter hint function.
There are also a lot of Java resources on the web, read the "Thinking in Java" in the first few chapters of the Chinese version, the point of view and found here:
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);

Run Result:
I=1 j=11
i=2 j=4
I=3 j=6
I=4 j=8

And after a little change:
for (int i=1, j=i+10 i<5; i++) {
j=i*2;
System.out.println ("i=" +i+ "j=" +j);
}

The result was:
I=1 j=2
i=2 j=4
I=3 j=6
I=4 j=8

After the online help found that the reason is: For loop after performing the conditional test, first executes the program part, and then only stepping.

2. Break and continue tagging usage
In the Java statement, the only place to put the label is in front of the loop statement. And there can't be anything between the loop statement and the label.
Label1:
outer-iteration {
inner-iteration {
//...
Break 1
//...
Continue 2
//...
Continue Label1; 3
//...
Break Label1; 4
}
}

Case 1 interrupts the internal loop. 2 interrupts the internal current loop and jumps directly into the next round of loops. Case 3 interrupts the internal and external loops, jumps to Label1, and restarts the loop from the external start. Case 4 jumps to the Label1 and does not enter the loop.

Here are some examples:

public class Labeledfor {
Static Test monitor = new test ();
public static void Main (string[] args) {
int i = 0;
Outer://Can ' t have statements here
for (; True;) {//Infinite loop
Inner://Can ' t have statements here
for (; i < 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;
}
}
}
}
}
} ///:~

Results:
"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"


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.