Hosting and domain costs until October 2024 have been generously sponsored by dumptruck_ds. Thank you!

Difference between revisions of "break"

From Quake Wiki

Line 1: Line 1:
 +
Syntax:<br/>
 +
<code>break;</code>
 +
 
This statement is used for flow control.  It is meant to be placed inside a loop (either do or while) and will cause execution to exit out of the loop and continue on.
 
This statement is used for flow control.  It is meant to be placed inside a loop (either do or while) and will cause execution to exit out of the loop and continue on.
  

Revision as of 00:34, 25 March 2013

Syntax:
break;

This statement is used for flow control. It is meant to be placed inside a loop (either do or while) and will cause execution to exit out of the loop and continue on.

Ex:

while(TRUE)
{
    x = x + 1;
    if (x > 5)
        break;
}

In this example, the condition in the while() loop will always evaluate to true, so this loop would normally never end. By placing a counter inside the loop and checking it's value, we manually break out of the loop by executing the break; statement when x is larger than 5.

In general, it is discouraged from overusing break as a properly written do or while loop should have it's own conditions for exiting and overuse of break, especially in large loops, can impact readability. This does not mean you should never use it, only to be sure it is actually needed.