File: //snap/gnome-3-38-2004/140/usr/share/devhelp/books/vala-0.48/Statements.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8" />
<title>Statements - Vala Reference Manual</title>
<link rel="stylesheet" type="text/css" href="default.css"><meta name="viewport" content="initial-scale=1">
</head>
<body>
<div class="o-fixedtop c-navbar"><div class="o-navbar">
<span class="c-pageturner u-float-left"><a href="index.html">Contents</a></span><span>Vala Reference Manual</span><div class="u-float-right">
<span class="c-pageturner o-inlinewidth-4"><a href="Expressions.html">Prev</a></span><span class="c-pageturner o-inlinewidth-4"><a href="Namespaces.html">Next</a></span>
</div>
</div></div>
<h2>5. Statements</h2>
<ul class="page_toc">
<li><a href="Statements.html#Simple_statements">5.1 Simple statements</a></li>
<li><a href="Statements.html#Variable_declaration">5.2 Variable declaration</a></li>
<li><a href="Statements.html#Selection_statements">5.3 Selection statements</a></li>
<li><a href="Statements.html#Iteration_statements">5.4 Iteration statements</a></li>
<li><a href="Statements.html#Jump_Statements">5.5 Jump Statements</a></li>
<li><a href="Statements.html#Try_Statement">5.6 Try Statement</a></li>
<li><a href="Statements.html#Lock_Statement">5.7 Lock Statement</a></li>
</ul>
<p>Statements define the path of execution within methods and similar constructions. They combine expressions together with structures for choosing between different code paths, repeating code sections, etc. </p>
<blockquote class="o-box c-rules">statement:
empty-statement
simple-statement
statement-block
variable-declaration-statement
if-statement
switch-statement
while-statement
do-statement
for-statement
foreach-statement
return-statement
throw-statement
try-statement
lock-statement
embedded-statement:
statement
</blockquote>
<h3 id="Simple_statements">5.1 Simple statements</h3>
<p>The Empty Statement does nothing, but is a valid statement nonetheless, and so can be used wherever a statement is required. </p>
<blockquote class="o-box c-rules">empty-statement:
<span class="literal">;</span>
</blockquote>
<p>A Simple Statement consists of one a subset of expressions that are considered free-standing. Not all expressions are allowed, only those that potentially have a useful side effect - for example, arithmetic expressions cannot form simple statements on their own, but are allowed as part of an assignment expressions, which has a useful side effect. </p>
<blockquote class="o-box c-rules">simple-statement:
statement-expression <span class="literal">;</span>
statement-expression:
assignment-expression
class-instantiation-expression
struct instantiation-expression
invocation-expression
</blockquote>
<p>A Statement Block allows several statements to be used in a context that would otherwise only allow one. </p>
<blockquote class="o-box c-rules">statement-block:
<span class="literal">{</span> [ statement-list ] <span class="literal">}</span>
statement-list:
statement [ statement-list ]
</blockquote>
<p>Blocks create anonymous, transient scopes. For more details about scopes, see <a href="http://wiki.gnome.org/Projects/Vala/Manual/Export/Projects/Vala/Manual/Concepts#Scope_and_naming">Concepts/Scope and naming</a>. </p>
<h3 id="Variable_declaration">5.2 Variable declaration</h3>
<p>Variable Declaration Statements define a local variable in current scope. The declaration includes a type, which signifies the variable will represent an instance of that type. Where the type can be inferred by the compiler, the type-name can be replaced with the literal "var" </p>
<blockquote class="o-box c-rules">variable-declaration-statement:
variable-declaration-with-explicit-type
variable-declaration-with-explicit-type-and-initialiser
variable-declaration-with-type-inference
variable-declaration-with-explicit-type:
type-name identifier <span class="literal">;</span>
variable-declaration-with-explicit-type-and-initialiser:
type-name identifier <span class="literal">=</span> expression <span class="literal">;</span>
variable-declaration-with-type-inference:
var identifier <span class="literal">=</span> expression <span class="literal">;</span>
</blockquote>
<p>Type inference is possible in any case where the variable is immediately assigned to. The type chosen will always be the type of the assigned expression, as decided by the rules described at <a href="http://wiki.gnome.org/Projects/Vala/Manual/Export/Projects/Vala/Manual/Expressions#">Expressions</a>. It is important to realise that the type of the variable will be fixed after the first assignment, and will not change on assigning another value to the variable. If the variable should be created with a type other than that of the assigned expression, the expression should be wrapped with a cast expression, provided that the cast is valid. </p>
<h3 id="Selection_statements">5.3 Selection statements</h3>
<p>The If Statement decides whether to execute a given statement based on the value of a boolean expression. There are two possible extensions to this model: </p>
<p>An else clause declares that a given statement should be run if-and-only-if the condition in the the if statement fails. </p>
<p>Any number of "else if" clauses may appear between the "if" statement and its "else" clause (if there is one.) These are equivalent to: </p>
<p>FIXME: This doesn't work. </p>
<p>In simple terms, the program will test the conditions of the if statement and its "else if" clauses in turn, executing the statement belonging to the first that succeeds, or running the else clause if every condition fails. </p>
<blockquote class="o-box c-rules">if-statement:
<span class="literal">if</span><span class="literal">(</span> boolean-expression <span class="literal">)</span> embedded-statement [ elseif-clauses ] [ <span class="literal">else</span> embedded-statement ]
elseif-clauses:
elseif-clause
[ elseif-clauses ]
elseif-clause:
<span class="literal">else if</span><span class="literal">(</span> boolean-expression <span class="literal">)</span> embedded-statement
</blockquote>
<p>The switch statement decides which of a set of statements to execute based on the value of an expression. A switch statement will lead to the execution of one or zero statements. The choice is made by: </p>
<blockquote class="o-box c-rules">switch-statement:
<span class="literal">switch</span><span class="literal">(</span> expression <span class="literal">)</span><span class="literal">{</span> [ case-clauses ] [ default-clause ] <span class="literal">}</span>
case-clauses:
case-clause
[ case-clauses ]
case-clause:
<span class="literal">case</span> literal-expression <span class="literal">:</span> embedded-statement
default-clause:
<span class="literal">default</span><span class="literal">:</span> embedded-statement
</blockquote>
<h3 id="Iteration_statements">5.4 Iteration statements</h3>
<p>Iteration statements are used to execute statements multiple times based on certain conditions. Iteration Statements contain loop embedded statements - a superset of embedded statements which adds statements for manipulating the iteration. </p>
<blockquote class="o-box c-rules">loop-embedded-statement:
loop-embedded-statement-block
embedded-statement
break-statement
continue-statement
loop-embedded-statement-block:
<span class="literal">{</span> [ loop-embedded-statement-list ] <span class="literal">}</span>
loop-embedded-statement-list:
loop-embedded-statement [ loop-embedded-statement-list ]
</blockquote>
<p>Both break and continue statement are types of jump statement, described in . </p>
<h4>The While Statement<a name="The_While_Statement"> </a>
</h4>
<p>The <code>while</code> statement conditionally executes an embedded statement zero or more times. When the while statement is reached, the boolean expression is executed. If the boolean value is true, the embedded statement is executed and execution returns to the <code>while</code> statement. If the boolean value is false, execution continues after the <code>while</code> statement. </p>
<blockquote class="o-box c-rules">while-statement:
<span class="literal">while</span><span class="literal">(</span> boolean-expression <span class="literal">)</span> loop-embedded-statement
</blockquote>
<p>The <code>do</code> statement conditionally executes an embedded statement one or more times. First the embedded statement is executed, and then the boolean expression is evaluated. If the boolean value is true, execution returns to the <code>do</code> statement. If the boolean value is false, execution continues after the <code>do</code> statement. </p>
<blockquote class="o-box c-rules">do-statement:
<span class="literal">do</span> loop-embedded-statement <span class="literal">while</span><span class="literal">(</span> boolean-expression <span class="literal">)</span><span class="literal">;</span>
</blockquote>
<h4>The For Statement<a name="The_For_Statement"> </a>
</h4>
<p>The <code>for</code> statement first evaluates a sequence of initialization expressions and then repeatedly executes an embedded statement. At the start of each iteration a boolean expression is evaluated, with a true value leading to the execution of the embedded statement, a false value leading to execution passing to the first statement following the <code>for</code> statement. After each iteration a sequence of iteration expressions are evaluated. Executing this type of statement creates a new transient scope, in which any variables declared in the initializer are created. </p>
<blockquote class="o-box c-rules">for-statement:
<span class="literal">for</span><span class="literal">(</span> [ for-initializer ] <span class="literal">;</span> [ for-condition ] <span class="literal">;</span> [ for-iterator ] <span class="literal">)</span> loop-embedded-statement
for-initializer:
variable-declaration [ <span class="literal">,</span> expression-list ]
for-condition:
boolean-expression
for-iterator:
expression-list
</blockquote>
<h4>The Foreach Statement<a name="The_Foreach_Statement"> </a>
</h4>
<p>The <code>foreach</code> statement enumerates the elements of a collection, executing an embedded statement for each element of the collection. Each element in turn is assigned to a variable with the given identifier and the embedded statement is executed. Executing this type of statement creates a new transient scope in which the variable representing the collection element exists. </p>
<blockquote class="o-box c-rules">foreach-statement:
<span class="literal">foreach</span><span class="literal">(</span> type identifier <span class="literal">in</span> expression <span class="literal">)</span> loop-embedded-statement
</blockquote>
<p>Foreach Statements are able to iterate over arrays and any class that implements the <code>Gee.Iterable</code> interface. This may change in future if an Iterable interface is incorporated into GLib. </p>
<h3 id="Jump_Statements">5.5 Jump Statements</h3>
<p>Jump statements move execution to an arbitrary point, dependent on the type of statement and its location. In any of these cases any transient scopes are ended appropriately: <a href="http://wiki.gnome.org/Projects/Vala/Manual/Export/Projects/Vala/Manual/Concepts#Scope_and_naming">Concepts/Scope and naming</a> and . </p>
<p>A <code>break</code> statement moves execution to the first statement after the nearest enclosing <code>while</code>, <code>do</code>, <code>for</code>, or <code>foreach</code> statement. </p>
<blockquote class="o-box c-rules">break-statement:
<span class="literal">break</span><span class="literal">;</span>
</blockquote>
<p>A <code>continue</code> statement immediately moves execution the nearest enclosing <code>while</code>, <code>do</code>, <code>for</code>, or <code>foreach</code> statement. </p>
<blockquote class="o-box c-rules">continue-statement:
<span class="literal">continue</span><span class="literal">;</span>
</blockquote>
<p>The <code>return</code> statement ends the execution of a method, and therefore completes the invocation of the method. The invocation expression has then been fully evaluated, and takes on the value of the expression in the <code>return</code> statement if there is one. </p>
<blockquote class="o-box c-rules">return-statement:
<span class="literal">return</span> [ expression ] <span class="literal">;</span>
</blockquote>
<p>The throw statement throws an exception. </p>
<blockquote class="o-box c-rules">throw-statement:
<span class="literal">throw</span> expression <span class="literal">;</span>
</blockquote>
<h3 id="Try_Statement">5.6 Try Statement</h3>
<p>The <code>try</code> statement provides a mechanism for catching exceptions that occur during execution of a block. Furthermore, the <code>try</code> statement provides the ability to specify a block of code that is always executed when control leaves the <code>try</code> statement. </p>
<p>For the syntax of the try statement, See <a href="http://wiki.gnome.org/Projects/Vala/Manual/Export/Projects/Vala/Manual/Errors#Error_catching">Errors/Error catching</a>. </p>
<h3 id="Lock_Statement">5.7 Lock Statement</h3>
<p>Lock statements are the main part of Vala's resource control mechanism. </p>
<p>FIXME: Haven't actually written anything here about resource control. </p>
<blockquote class="o-box c-rules">lock-statement:
<span class="literal">lock</span><span class="literal">(</span> identifier <span class="literal">)</span> embedded-statement
</blockquote>
</body>
</html>