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

Difference between revisions of "break"

From Quake Wiki

 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
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.
+
''void'' '''break'''()
  
Ex:
+
== Usage ==
<pre>while(TRUE)
+
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.
{
 
&nbsp;&nbsp;&nbsp;&nbsp;x = x + 1;
 
&nbsp;&nbsp;&nbsp;&nbsp;if (x > 5)
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
 
}
 
</pre>
 
  
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.
+
== Example ==
 
+
for (float i = 0; i < cap; ++i)
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.
+
{
 +
    PerformFirstTask();
 +
   
 +
    // If the entity died in the first task, exit the loop early
 +
    if (self.health <= 0)
 +
        break;
 +
 +
    PerformSecondTask();
 +
  }
  
 
[[Category:QuakeC Function]]
 
[[Category:QuakeC Function]]

Latest revision as of 10:55, 31 July 2023

void break()

Usage[edit]

Breaks out of a loop similar to the standard break keyword in other programming languages. This is largely deprecated in favor of using break as a keyword in modern QuakeC compilers.

Example[edit]

for (float i = 0; i < cap; ++i)
{
    PerformFirstTask();

    // If the entity died in the first task, exit the loop early
    if (self.health <= 0)
        break;

    PerformSecondTask();
}