Flow Control
Control-flow statements allow you to regulate the flow of your script's execution. Using control statements, you can write GPC code that makes decisions and repeats actions.
1. if
The if construct is one of the most important features of many languages, it allows for conditional execution of code fragments. GPC language features an if structure that is similar to that of C:
if( expression ) {
statement
}
Expression is evaluated to its boolean value, if expression evaluates to TRUE, GPC will execute statement, and if it evaluates to FALSE the statement will be ignored. If statements can be nested infinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program. Some examples:
if(a == b)
a = 10;
if( a < b ) {
a = b;
}
if( b ) {
if( a < c ) a = 20;
b = a;
}
|
2. else
Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if expression evaluates to FALSE. For example, the following code would use the a value if a is greater than b, or use b value otherwise.
if( a > b )
set_val(XB360_RT, a);
else
set_val(XB360_RT, b);
|
3. else if
else if, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the else if conditional expression evaluates to TRUE.
if( a > b )
set_val(XB360_RT, a);
else if( b > a )
set_val(XB360_RT, b);
else
set_val(XB360_RT, 0);
|
4. while
while loops are the only type of loop in GPC, in fact there is no need of loops in GPC scripts since the main procedure acts like one. Is very probable you'll be doing something wrong if you are using while in your script. The basic form of a while statement is:
while( expression ) {
statement
}
The operation of a while is quite simple, it repeatedly executes the nested statement(s) as long as the conditional expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), the execution will not stop until the end of the iteration. The break command can be used to jump out of the current while, as show in the following example:
int x;
main {
x = 10;
while( x > 0 ) {
x = x - 1;
if(get_val(PS3_CROSS)) {
break;
}
}
}
|