Toggle menu
875
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Using break to Exit a Loop

From Catglobe Wiki
Revision as of 08:30, 21 March 2011 by Cg_pham (talk | contribs)



Using break to Exit a Loop

It is possible to force an immediate exit from a loop, bypassing any code remaining in the body of the loop and the loop’s conditional test, by using break statement. When a break statement is encountered inside a loop, the loop is terminated, and program control resumes at the next statement following the loop. Here is an example:

{

number i = 1;

while (i<10)

{

if (i==6) break;

print(i);

i = i + 1;

}

print("Counting completed.");

}

The output from the script is shown here:

1

2

3

4

5

Counting completed.

In nested loops, the break statement in the inner loop causes only the termination of that loop. The outer loop is unaffected.