<?xml version="1.0" ?>
<?xml-stylesheet type="text/css" href="stylesheet.css" ?>

<!-- Copyright 2003 Steve Folta -->

<cleet-doc>

<title> Statements </title>


<header> Basic Statements </header>

<p>
The most common type of statement is an expression followed by a semicolon:
</p>

<code>some-object.do-something();</code>

<p>
Statements can be grouped together using braces:
</p>

<code>{
  some-object.do-something();
  x = y + 1;
}</code>

<p>
The "if" statement works like in C:
</p>

<code>if (a-string.is-empty) {
  a-string = "&lt;empty&gt;";
  }</code>



<header> Declarations </header>

<p>
Local variables are declared by following the name of the new variable
with a colon and the type of the variable.  The statement ends with a
semicolon:
</p>

<code>name: String;</code>

<p>
You can initialize the variable to a value:
</p>

<code>name: String = "Fred";</code>

<p>
The declaration can also create a new object.  You can give the
arguments to a <id>create()</id> function.  These two statements are
equivalent:
</p>

<code>node: Node(value);
node: Node = new Node(value);</code>

<p>
Let's return to the first example:
</p>

<code>name: String;</code>

<p>
What is the value of <id>name</id>?  If the class of the new variable has a
<id>create()</id> function with no arguments (as <id>String</id> does),
a new object is created and that <id>create()</id> function is called. 
If not, it is set to nil.  So, assuming there's a <id>Node</id> class
that <i>doesn't</i> have a nullary <id>create()</id> function, the
following pairs of statements are equivalent:
</p>

<code>name: String;
name: String();

node: Node;
node: Node = nil;</code>


<header> Looping Statements </header>

<p>
The most basic kind of loop is the "loop" statement.  It repeats forever:
</p>

<code>loop {
  do-something();
  }</code>

<p>
This wouldn't be very useful, except you can use the "break" statement to
break out of the loop.  You can also use the "continue" statement to
skip to the top of the loop.
</p>

<p>
The "while" and "do/while" statements are also available:
</p>

<code>while (y &lt; bottom) {
  draw-line(x, y);
  y = y + line-height;
  }

do {
  line: String = stream.read-line();
  (line + "\n").write();
} while (!stream.is-done);</code>

<p>
The most complex looping statement is the "for" statement.  The Cleet
"for" statement is completely different from the one in C/C++.  It's used
for iterating over collections:
</p>

<code>sum: Int = 0;
for (value: Int in array)
  sum += value;

for (line: String in file.contents.lines) {
  if (line.is-empty)
    continue;
  parse-line(line);
  }</code>

<p>
As you can see, the "for" statement declares a new local variable (whose
scope is the loop), and sets it to each value in the collection in turn.
How does this work?  The collection will have a function called
<id>iterator</id> that returns an iterator object.  So the first example
is the same as this:
</p>

<code>{
  sum: Int = 0;
  iterator: IntArrayIterator = array.iterator;
  while (!iterator.is-done) {
    value: Int = iterator.current-item;
    sum += value;
    iterator.go-forward();
    }
}</code>

<p>
And for the more detail-oriented among you, here's the equivalent of the
second example:
</p>

<code>{
  iterator: LinesIterator = file.contents.lines.iterator;
  while (!iterator.is-done) {
    line: String = iterator.current-item;
    try {
      if (line.is-empty)
        throw ContinueException;
      parse-line(line);
      }
    catch (ContinueException) {
      <i>// just keep going</i>
      }
    iterator.go-forward();
    }
}</code>


<header> Exceptions </header>

<p>
As that last example shows, Cleet supports throwing and catching
exceptions with the "throw" and "try"/"catch" statements.  "throw" can
either take an existing object or it can create a new object.  The
following two statements are equivalent:
</p>

<code>throw MessageException("Something is terribly wrong!");
throw new MessageException("Something is terribly wrong!");</code>

<p>
The "catch" statement shown above doesn't actually do anything with the
object it caught, but you can:
</p>

<code>try {
  do-something();
  }
catch (exception: Exception) {
  ("Error!: " + exception.message + "\n").write();
  }</code>

<p>
Any type of object can be thrown or caught, but there is a standard
<id>Exception</id> class that most exceptions will be subclasses of. 
There is also a simple <id>MessageException</id> class that is easy to
throw.
</p>


<header> Returning from a Method </header>

<p>
Finally, there is a "return" statement to return from a function:
</p>

<code>return;
return some-value;</code>


</cleet-doc>

