The use of break continue and return in for cycles in Java

  • 2020-11-18 06:14:53
  • OfStack

Introduction: When using a loop, break, continue and return are often mixed up in the loop. Today, I have arranged them 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;// Termination of while cycle , Continue to for The following code ; (Terminate the current (while) Loop, continue to loop the rest of the code)  
} 
}

Print results:


i==for=>1 
i==for=>2 
i==while==>2 
i==for=>3 
i==for=>4 
i==while==>4
for (int i = 1; i < 5; i++) { 
System.out.println( Outer cycle i==> " +i); 
for (int j = 1; j < 5; j++) { 
System.out.println( "Inner circulation j==> " +j); 
while(j==2){ 
break;// Termination of while cycle , Continue to for The following code ; (Terminate the current (while) Loop, continue to loop the rest of the code)  
} 
} 
}

Print results:


 The outer loop i==>1 
 The inner loop j==>1 
 The inner loop j==>2 
 The inner loop j==>3 
 The inner loop j==>4 
 The outer loop i==>2 
 The inner loop j==>1 
 The inner loop j==>2 
 The inner loop j==>3 
 The inner loop j==>4 
 The outer loop i==>3 
 The inner loop j==>1 
 The inner loop j==>2 
 The inner loop j==>3 
 The inner loop j==>4 
 The outer loop i==>4 
 The inner loop j==>1 
 The inner loop j==>2 
 The inner loop j==>3 
 The inner loop j==>4
for (int i = 1; i < 5; i++) { 
System.out.println( Outer cycle i==> " +i); 
for (int j = 1; j < 5; j++) { 
System.out.println( "Inner circulation j==> " +j); 
if(j==2){ 
System.out.println( "The inner for End of loop..." ); 
break;// Terminates the current inner layer for cycle , Continue to the outer for The following code ; (Terminates the current loop and continues the code after the outer loop)  
} 
System.out.println( " j==> " +j); 
} 
}

Print results:


 The outer loop i==>1 
 The inner loop j==>1 
j==>1 
 The inner loop j==>2 
 The inner layer for End of loop...  
 The outer loop i==>2 
 The inner loop j==>1 
j==>1 
 The inner loop j==>2 
 The inner layer for End of loop...  
 The outer loop i==>3 
 The inner loop j==>1 
j==>1 
 The inner loop j==>2 
 The inner layer for End of loop...  
 The outer loop i==>4 
 The inner loop j==>1 
j==>1 
 The inner loop j==>2

Inner for loop ends...


for (int i = 1; i < 5; i++) { 
while(i%2==0){ 
System.out.println( " i==return==> " +i); 
return;// Terminates the currently executing function , The following code will not be executed  
} 
System.out.println( " i====> " +i); 
}

Print results:


i====>1 
i==return==>2

for (int i = 1; i < 5; i++) { 
System.out.println( " i===for=> " +i); 
while(i%2==0){// This cycle is 1 An infinite loop  
System.out.println( " i==while==> " +i); 
continue;// Termination of this while The loop , Continue to while The following code ; (End the loop and continue the loop)  
} 
System.out.println( " i===> " +i); 
}

Print results:


i==for=>1 
i===>1 
i==for=>2 
i==while==>2 
i==while==>2 
i==while==>2 
. 
. 
.

From the above results, the bottom line is that break breaks out of the current loop (the closest one) and continues the outer loop; continue is the end of the loop, the code after continue does not execute, continue the loop behind, that is, it is still in the same loop, different from break, break is to jump to the outer loop; return terminates the current method, and none of the code following the method will be executed. These are only the results of my rough test. If you have any good additions, please leave a message and I will modify them accordingly.

conclusion


Related articles: