Control Structures

Control structures are statements that affect the flow of the program, branching on conditions and repeating blocks of code.

  1. 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
    {
    }
    
  2. 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!
    
  3. 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.","","");
    }
  4. 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.

  5. 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!
    
  6. 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++;
    }
    
Talkback

Post a new comment?

Talkback #2 written by zaril on 2005-07-02.

in gayo's case the if (0) won't execute simply because he put it after a never-ending loop that will execute. to be perfectly clear you should see them as different snippets of code, not one. example:

if (1)
{
// this will always execute
}

...and...

if (0)
{
// this will never execute
}

Talkback #1 written by Gayo on 2004-07-15.

Integer values are used in if and while statements. A nonzero integer value constitutes a true statement; a zero integer value constitutes a false statement. Thus:

if (1)
{
     // This clause will always execute
}
if (0)
{
    // This clause will never execute
}
Rather than having a constant or a logical statement as the condition in a conditional statement, you can have a function that returns an integer value. In this case, the function will be executed, and the clause will occur if the value returned was nonzero.

Post a new comment?

Doc Nav

Your docs
View All Docs

If you log in, you can edit the documentation, or create your own documents and tutorials!

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.