| This article describes how to use php to jump out of the loop break and skip a certain layer of loop continue. For more information, see.
This article will show you how to use break and continue and their functions in loop statements.
1,Break (int); statement// The role of break is to exit the loop body. when a number is added after the break, it indicates to exit the loop of the nth layer. Mostly in the while, do... While, for, foreach, switch statement.
If a number is added to the end of the break, the execution process of the loop statement changes.
Code description: $ I = 1 $ j = 1 to end the loop. When we perform the first loop, when $ I = 1, we start to execute the while code at the first layer of the loop body, following the output of $ I = 1, and enter the layer 2 while loop. After if judgment, $ j is not equal to 2 and does not execute break 2 in the if body. this statement block is used to execute the output statement and output $ j = 1. then, the second judgment of the second layer loop is performed, then $ j = 2 executes break 2 in the if body;
Note: Here, the break is followed by the number 2, indicating that the two-layer loop body code is exited, that is, the first and second-layer loop bodies jump out of the judgment loop, the above output result is: $ I = 1 $ j = 1.
2,Continue (int); statement// The execution result of the continue statement is similar to that of the break statement. The continue skips several layers of loops, ignores the code segments in the skip loop body, and executes the next loop directly at the end of the loop body, similar to the break statement... While, for, foreach, switch statement.
Code description: if the if judgment statement is not added to the while loop body, the output result is: $ I = 1 $ I = 2 $ I = 3 $ I = 4; however, when an if judgment statement is added, the output result is: $ I = 1 $ I = 3 $ I = 4, which is the impact of the continue statement, the continue statement skips the while loop body statement when $ j ++ returns 2, that is, when $ j = 2, return directly to the end of the loop body (that is, jump to the head of the loop body) to continue the next loop operation, when a number is added to the end of a continue statement, the output result is changed.