Control structures are statements that affect the flow of the program, branching on conditions and repeating blocks of code.
if (and else): if statements are the most basic form of code execution control. Code is conditionally executed, depending on whether condition is true.
if(condition) { // code when condition is true }
If statements may optionally be followed by an else clause, which allows code to happen if a condition is false.
if(condition) { // code when condition is true } else { // code when condition is false }
The braces on if/else statements are optional. (but you can only have one statement if you forget the braces):
if(condition) statement; else statement;
You can chain a bunch of if/else together for more complex things:
if(condition) { } else if(condtion) { } else if(conditon) { } else { }
unless: Unless statements are similar to if statements, except with theirs condition negated. This causes them to execute when their condition is false:
unless(condition) statement; // This... if(!condition) statement; // ...is the same as this!
switch: Switch statements are most useful in situations where you wish to provide multiple results based on the condition of some variable. They are basically used to replace a series of if statements. Here is their syntax:
switch(value) { case result1: statement; case result2: statement; case result3: statement; default: statement; }
When the interpreter encounters this construct, it will test the value of what is given in the switch. It will then run through the cases until it finds one that matches it, if it finds any. But if no case matches the value, it will use the default case, assuming one exists (a default case is optional).
For people from a C background, note that unlike C, cases inside a switch can be expressions or functions that are evaluated at runtime. Also unlike C, no break; statements are used; break statements in VC are not only unnecessary but will cause an error if they are present.
Here is an example:
// Checking the first person in the party... switch(party[0]) { case 1: Text(DARIN,"My name's Darin and I'm leading this party!","",""); case 2: Text(SARA,"I'm Sara and I'm here to pump you up!","",""); case 3: Text(DEXTER,"Dexter is leading this party.","",""); }
while: While statements are similar to if statements, except that they will be repeated as long their condition is true. This makes them capable of looping.
while(condition) { // code run condition while true }
Remember: VC will repeatedly execute the loop until condition is false. If your condition can never become false, you get an infinite loop, so be careful.
until: Until statements are like while statements, except with their condition negated. This causes them to execute as long as their condition is false.
until(condition) statement; // This... while(!condition) statement; // ...is the same as this!
for: For loops are the other kind of loop statement that VC supports. Here is their syntax.
for(init; condition; post) { // code run while condition is true. }
The init part is a statement that initializes a variable before the loop starts. The loop will repeat as long as it condition is true. The post part is a statement that changes a variable after each pass through the loop.
Here is an example:
for(i = 0; i < 5; i++) { PrintString(0, 0, screen, i, "Test " + str(i) + "."); }
This is the same as the following code:
i = 0; while(i < 5) { PrintString(0, 0, screen, i, "Test " + str(i) + "."); i++; }