A Quick Introduction to Virgil - Statements

This is part of a basic introduction to programming in Virgil and gives an overview of the language, its syntax, and its structures. This page focuses on the syntax of the basic statements in Virgil.

Overview

program MyProgram {
    entrypoint main = MyComp.main;
}
component MyComp {
    method main(arg: char[][]): int {
        // statements go here.
    }
}

Virgil is basically a procedural language like Java, C, C++ and has very similar syntax and control structures such as if, for, while, and switch. Statements appear within the body of declared methods. This tutorial covers declaring and using methods in more detail later, but consider the outline of a Virgil program at the right. The body of the main() method can contain the statements that implement our desired functionality. Statements are generally written one per line and are terminated with a semicolon ;.

Declaring Local Variables

local var: int;           // var of type int
local foo: int = 0;       // foo of type int
local bar: int, baz: int; // bar and baz
local inf = 3 + var;      // inferred type int
local bool = false;       // inferred type boolean
local err;                // ERROR

Local variables are declared with a statement beginning with the keyword local, followed by the name of the variable. Virgil is statically typed, meaning that each variable must be given a type at its declaration. We can give a variable a type either explicitly by specifying the type in the declaration, or by supplying an initialization expression. We can specify the type explicitly by a colon : and then the type, or implicitly by an assignment = followed by an expression. If no type is explicitly given, the compiler will infer the type of the variable to be the result of the initialization expression. The compiler will reject local declarations that have neither an explicit type nor an initialization expression.

Blocks and Scope

Virgil allows multiple statements to be grouped into a single statement with the curly braces { ... }. This allows a sequence of statements to be used as the body of a loop or as a branch of an if or switch statement. Blocks introduce a new scope for local variables. A new local variable declared inside a block is visible from its declaration point until the end of the enclosing block.

Assignments

var = expr;

Assignment statements update the value of a local variable or parameter with a new value. The update overwrites the variable with the new value, and the old value is gone.

The If Statement

if ( expr ) stmt; else stmt;
if ( expr ) stmt;

The if statement branches on the result of an expression. The expression that represents the condition must be of type boolean. If the condition expression evaluates to the value true, then the first statement is executed, otherwise the second statement is executed. The else clause is optional. After executing the correct branch of the statement, control continues to the statement following the if.

The While Loop

while ( expr ) stmt;

The while loop is the simplest of all the loop constructs in Virgil. The expression that represents the condition is required to be of type boolean. Upon entry to the while statement, the condition expression is evaluated. If the condition evaluates to true the statement that represents the body of the loop is executed. Each time control reaches the end of the loop body, the condition is reevaluated. If true, the loop body executes again. Thus, the while loop will repeatedly execute the body until the condition evaluates to false.

The Do...While Loop

do stmt while ( expr );

The do...while loop is similar to the while loop, except that the loop body is always executed at least once before evaluating the condition. The expression that represents the condition is required to be of type boolean. When control reaches the end of the loop body, the condition is evaluated. If the condition evaluates to true, the loop body is executed again. Thus, the do...while loop will repeatedly execute the body until the condition evaluates to false.

The For Loop

for ( expr; expr; expr ) stmt;

The for loop is the most complex looping construct in Virgil. The first expression represents an initialization expression for the loop; the second expression represents a condition expression (required to be of type boolean), and the third expression represents the loop update expression. Executing a for statement begins with the initialization expression, which is evaluated only once. Then the second expression (the condition) is evaluated. If it evaluates to true, the statement representing the loop body is executed; if false, control passes to the statement following the loop. After the loop body statement has been executed, the update expression is executed and then the condition is reevaluated; the loop is repeated until the condition evaluates to false.

The Switch Statement

switch ( expr ) { 
   case (value) stmt; 
   case (value, value) stmt;
   default stmt;
}

The switch statement is a multi-way branch statement. When the switch statement is executed, the first expression is evaluated, producing a value. The value is then compared against the values for each of the cases. If the value matches the value in a declared case, then the statement of that case is executed, and then control passes automatically to the end of the switch statement. If no value in the cases matches and a default case is present, then the statement associated with the default case is executed.

(Note: Unlike C or Java, the cases of a switch statement do not automatically fall through to the next case. Instead, each case statement implicitly breaks to the end of the switch. Multiple values can be specified for a single branch.)

The Break Statement

break;

The break statement is a loop control statement, and can only legally occur inside the body of some loop construct such as a while, do...while, or for loop. When executed, the break statement causes control to immediately pass to the end of the nearest enclosing loop.

(Note: Unlike C or Java, the break statement is not used inside of switch statements to break to the end of the statement).

The Continue Statement

continue;

The continue statement is a loop control statement, and can only legally occur inside the body of some loop construct such as a while, do...while, or for loop. When executed, the continue statement causes control to immediately pass to the beginning of the nearest enclosing loop. For a while loop, this means going back to reevaluate the condition; for a do...while, this means going to the beginning of the loop block, and for a for loop, this means going to the update clause.

The Return Statement

return expr;
return;

The return statement is a method control statement. When executed, the return statement evaluates the expression representing the return value and then immediately returns out of the current method to the method's caller, returning the value of the evaluated expression. For methods with a declared return value, the type of the expression must match the declared return type of the method. For methods without a declared return type, the return statement must not have an expression.

Go back to the tutorial.