Introduction: When using loops, the loops often get confused when they have break, continue, and return. Today I specially sorted it out and wait for later use...
for (int i = 1; i < 5; i++) { System.out.println("i==for=>"+i); while(i%2==0){ System.out.println("i==while==>"+i); break;//Stop the while loop and continue the code behind; (Stop the current (while) loop and continue the code behind the loop) } }Print result:
i==for=>1 i==for=>2 i==while==>2 i==for=>3 i==for=>4 i==while==>4for (int i = 1; i < 5; i++) { System.out.println("outer loop i==>"+i); for (int j = 1; j < 5; j++) { System.out.println("inner loop j==>"+j); while(j==2){ break;//Stop the while loop and continue the code behind; (Stop the current (while) loop and continue the code behind the loop) } } } }Print result:
Outer loop i==>1 Inner loop j==>1 Inner loop j==>2 Inner loop j==>3 Inner loop j==>4 Outer loop i==>2 Inner loop j==>1 Inner loop j==>2 Inner loop j==>3 Inner loop j==>4 Outer loop i==>3 Inner loop j==>1 Inner loop j==>2 Inner loop j==>3 Inner loop j==>4 Outer loop i==>4 Inner loop j==>1 Inner loop j==>2 Inner loop j==>3 Inner loop j==>3 Inner loop j==>4 For (int i = 1; i < 5; i++) { System.out.println("Outer loop i==>"+i); for (int j = 1; j < 5; j++) { System.out.println("Inner loop j==>"+j); if(j==2){ System.out.println("Inner for loop end..."); break;//Terminate the current inner for loop and continue the code behind the outer for; (Terminate the current loop and continue the code behind the outer loop) } System.out.println("j==>"+j); } }Print result:
Outer loop i==>1 inner loop j==>1 j==>1 inner loop j==>2 inner for loop ends... Outer loop i==>2 inner loop j==>1 j==>1 inner loop j==>2 inner for loop ends... Outer loop i==>3 inner loop j==>1 j==>1 inner loop j==>2 inner for loop ends... Outer loop i==>4 inner loop j==>1 j==>1 inner loop j==>1 inner loop j==>2
The inner for loop ends...
for (int i = 1; i < 5; i++) { while(i%2==0){ System.out.println("i==return==>"+i); return;// Terminate the currently executed function, the subsequent code will not be executed} System.out.println("i====>"+i); }Print result:
i====>1 i===return==>2for (int i = 1; i < 5; i++) { System.out.println("i===for=>"+i); while(i%2==0){//This loop is a dead loop System.out.println("i==while==>"+i); continue;// Terminate this while loop and continue the code behind while; (End this loop and continue looping the code) } System.out.println("i===>"+i); }Print result:
i==for=>1 i===>1 i==for=>2 i==while==>2 i==while==>2 i==while==>2 i==while==>2 . . .
Judging from the above results, in general, break jumps out of the current loop (the closest loop) and continues the outer loop; continues to end this loop, the code after the continue is not executed, and continues to the subsequent loop, that is, it is still in the same loop, which is different from break, break jumps to the outer loop; return is terminated by the current method, and the code after the method will not be executed. These are just the results of my rough test. If you have any good supplements, please leave a message and I will make corresponding modifications.
Summarize
The above is the use of break, continue, and return in Java in the for loop introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!