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

Editing break

From Quake Wiki

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
''void'' '''break'''()
+
Syntax:<br/>
 +
<code>break;</code>
  
== Usage ==
+
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.
Breaks out of a loop similar to the standard <code>break</code> keyword in other programming languages. This is largely deprecated in favor of using <code>break</code> as a keyword in modern QuakeC compilers.
 
  
== Example ==
+
Ex:
for (float i = 0; i < cap; ++i)
+
<pre>while(TRUE)
{
+
{
    PerformFirstTask();
+
&nbsp;&nbsp;&nbsp;&nbsp;x = x + 1;
+
&nbsp;&nbsp;&nbsp;&nbsp;if (x > 5)
    // If the entity died in the first task, exit the loop early
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
    if (self.health <= 0)
+
}
        break;
+
</pre>
+
 
    PerformSecondTask();
+
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.
  
 
[[Category:QuakeC Function]]
 
[[Category:QuakeC Function]]

Please note that all contributions to Quake Wiki are considered to be released under the GNU Free Documentation License 1.3 or later (see Quake Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel | Editing help (opens in new window)