Break

The Break statement is used within a For or While to break the loop and terminate it by skipping the remaining codes within the loop and continuing the execution of code outside the loop.

This statement is used with an If statement within the loop to specify the test condition, where the break is performed if the condition is met.


Example

There are 19 rounds of apple-giving, 4 apples are given per round while the basket can only hold a maximum of 50 apples. Break function can we used to find out the final number of apples collected in the basket.

Break used in a Foreach Loop
Figure: Break used in a Foreach Loop

results matching ""

    No results matching ""