Example of using php loop statement to control break and continue

Source: Internet
Author: User
Example of using php loop statement to control break and continue
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.


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.