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

Editing QuakeC Loops and Conditions

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 29: Line 29:
 
       statements
 
       statements
 
     }while( expression )
 
     }while( expression )
 
= Extended syntax =
 
 
== For-loop construct ==
 
 
This is an extension supported by most modern qc compilers, basically every qcc since (and including) FrikQCC.
 
A more complex 'do everything' loop.
 
 
    for (initialassignment ; conditionexpression ; incrementexpression)
 
    {
 
      statements
 
    }
 
 
Which expands to:
 
 
    initialassignment;
 
    while  (conditionexpression)
 
    {
 
      statements
 
      incrementexpression;
 
    }
 
 
Note that the initialassignment and incrementexpression can include the comma operator, which allows you to separate multiple statements within a single expression.
 
 
The line 'for (i = 0; i < 64; i = i+1)' will thus execute the following code block with i set to the values 0 .. 63 inclusive. i will contain the value 64 after the loop has finished.
 
 
Slightly more usefully, the line 'for (e = find(world, classname, "player") ; e ; e = find(e, classname, "player") )' will execute its code block with e set to every single entity that has classname "player".
 
 
== Continue Statement ==
 
 
An extended statement available in every qc compiler since FrikQCC.
 
 
A continue statement takes the form:
 
    continue;
 
When executed, such a statement will jump to the condition part of the containing do-while,  or while loops, and will jump to the increment part of a for-loop (see the 'expanded' for-loop example).
 
 
Unlike break, continue is unaffected by switch constructs.
 
 
== Break statement ==
 
 
An extended statement available in every qc compiler since FrikQCC.
 
A break statement takes the form:
 
    break;
 
When executed, such a statement will jump out of the most inner loop for switch statement.
 
 
Note: not to be confused with the break() builtin, which behaves completely differently.
 
 
== Switch Construct ==
 
 
    switch(expression)
 
    {
 
    case CONSTANT_1:
 
      statements_1
 
      break;
 
    case CONSTANT_2:
 
      statements_2
 
    default:
 
      statements_def
 
      break;
 
    case CONSTANT_3 .. CONSTANT_4:
 
      statements_range
 
      break;
 
    }
 
 
If the expression evaluates to CONSTANT_1, the statements_1 line will be executed, followed by the break statement which ends the switch.
 
If it equals CONSTANT_2, statements_2 will be executed, followed by statements_def, followed by the default's break statement ending the switch. This is refered to as 'falling through' the end of the CONSTANT_2 case, and while perfectly legal is often unintended and best avoided.
 
If the expression's value is inclusively between CONSTANT_3 and CONSTANT_4, statements_range will be executed, before the break statement ends the switch. This is only valid if the expression evaluates to a float.
 
If the expression didn't match *any* value, then statements_def will be executed before the following break ends the switch.
 

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)