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

QuakeC Loops and Conditions

From Quake Wiki

Revision as of 18:23, 14 June 2013 by 86.183.25.94 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Loops and conditions[edit]

Conditional construct[edit]

   if( expression )
   {
     statements
   }
   else
   {
     statements
   }


Loop construct[edit]

Pre-test loop: Checks the expression before executing the code within the loop.

   while( expression )
   {
     statements
   }

or post-test loop: Check the expression after the first execution.

   do
   { 
     statements
   }while( expression )

Extended syntax[edit]

For-loop construct[edit]

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[edit]

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[edit]

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[edit]

   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.