【Foreword】
Student question: What are the differences and usage scenarios of return, break, and continue?
【Introduction】
Regarding return, break, and continue, I have summarized them in the lecture. It may not be very detailed, and some students do not understand it very well. Here, I will introduce the specific distinctions.
① break: jump out of the current loop , the loop layer where it is located ends
②continue: just skip this execution and continue to execute the next loop without jumping out
③return directly ends the entire method, no matter how many layers of loops the return is in
【Details】
Case: (I use PHP here, but the for loop syntax is similar to JS)
<?php for($i = 1;$i < 4; $i++){ for($j=1;$j < 3; $j++){ echo "I am the ".$i." row ".$j." column<br>"; } echo "第".$i."排<br>"; } echo "Complete the loop, the execution is complete"; ?>
Output result:
I'm 1st row 1st column
I'm in row 1, column 2
row 1
I am 2nd row 1st column
I'm in row 2, column 2
Row 2
I'm in row 3, column 1
I'm in row 3, column 2
row 3
Complete the loop, the execution is complete
This is the result of normal output. The following are executed with break, continue and return respectively
(1)break
Jump out of the current loop , and the loop layer you are in ends. The loop layer is for nested loops. If break is used in a certain layer of the nested loop, it will only jump out of the current layer and continue to execute other layers of the nested loop.
Modify the above code to see the effect:
<?php for($i = 1;$i < 4; $i++){ for($j=1;$j < 3; $j++){ if($i == 3){ break; } echo "I am the ".$i." row ".$j." column<br>"; } echo "第".$i."排<br>"; } echo "Complete the loop, the execution is complete"; ?>Output result:
I'm 1st row 1st column
I'm in row 1, column 2
row 1
I am 2nd row 1st column
I'm in row 2, column 2
Row 2
row 3
Complete the loop, the execution is complete
It can be analyzed from the above code: break just jumps out of the loop of the layer where it is located. In this example, it jumps out of the layer where the variable $j is located.
(2)continue
The function of continue is somewhat similar to break, the difference is that continue just skips the current loop and continues to execute the next loop, without jumping out
<?php for($i = 1;$i < 4; $i++){ for($j=1;$j < 3; $j++){ if($i == 3){ continue; } echo "I am the ".$i." row ".$j." column<br>"; } echo "第".$i."排<br>"; } echo "Complete the loop, the execution is complete"; ?>Output result:
row 1
I am 2nd row 1st column
I'm in row 2, column 2
Row 2
I'm in row 3, column 1
I'm in row 3, column 2
row 3
Complete the loop, the execution is complete
Analysis can be concluded that contain just skips this execution, and does not jump out of the loop
(3)return
The return keyword is not specifically used to jump out of a loop, the function of return is to end a method. Once a return statement is executed in the body of the loop, the return statement will end the method, and the loop will naturally end with it. Unlike continue and break, return directly ends the entire method, no matter how many levels of loops the return is in.
<?php for($i = 1;$i < 4; $i++){ for($j=1;$j < 3; $j++){ if($i == 2){ return; } echo "I am the ".$i." row ".$j." column<br>"; } echo "第".$i."排<br>"; } echo "Complete the loop, the execution is complete"; ?>
Output result:
I'm 1st row 1st column
I'm in row 1, column 2
row 1
Analysis can see that return directly ends the entire method, no matter how many layers of loops the return is in.
.