v10.50 v10.55 Alpha v10.54 Alpha v10.53 Alpha v10.52 Alpha v10.51 Alpha
v10.50 v10.55 Alpha v10.54 Alpha v10.53 Alpha v10.52 Alpha v10.51 Alpha

Statements

Statements are the building blocks of a program. A program is made up of list of statements.

Seven kinds of statements are used in CSL: expression statements, if statements, if-else statements, while statements, for statements, return statements, and compound statements.

Expression Statements

An expression statement describes a value, variable, or function.

<expression>

Here are some examples of the different kinds of expression statements:

C++
Value: x + 3; 

Variable: x = 3;

Function: Trace ( x + 3 );

The variable expression statement is also called an assignment statement, because it assigns a value to a variable.

if Statements

An if statement follows the form

if <expression> <statement>

For example,

if (3 && 3) Trace("True!");

will cause the program to evaluate whether the expression 3 && 3 is nonzero, or True. It is, so the expression evaluates to True and the Trace statement will be executed. On the other hand, the expression 3 && 0 is not nonzero, so it would evaluate to False, and the statement wouldn't be executed.

if-else Statements

The form for an if-else statement is

C++
if <expression> <statement1>
else <statement2>

The following code

C++
if ( 3 - 3 || 2 - 2 ) Trace ( "Yes" );
else Trace ( "No" );

will cause “No” to be printed, because 3 - 3 || 2 - 2 will evaluate to False

(neither 3 - 3 nor 2 - 2 is nonzero).

while Statements

A while statement is written as

while <expression> <statement>

An example of this is

C++
x = 2;
while ( x < 5 )
{
  Trace ( x, ", " );
  x = x + 1;
}

The result of this would be

2, 3, 4,

for Statements

A for statement takes the form

C++
for (<expression1>; <expression2>; <expression3>)
<statement>

The first expression initializes, or sets, the starting value for x. It is executed one time, before the loop begins. The second expression is a conditional expression. It determines whether the loop will continue: if it evaluates true, the function keeps executing and proceeds to the statement; if it evaluates false, the loop ends. The third expression is executed after every iteration of the statement.

att_1_for_326434906.png
Execution of a for statement

The example

for ( x = 2; x < 5; x = x + 1 ) Trace ( x, "\n" );

would output

C++
2
3
4

The example above works out like this: the expression x = 2 is executed. The value of x is passed to x < 5, resulting in 2 < 5. This evaluates to true, so the statement Trace (x, "\n" ) is performed, causing 2 and a new line to print. Next, the third expression is executed, and the value of x is increased to 3. Now,

x < 5 is executed again, and is again true, so the Trace statement is executed, causing 3 and a new line to print. The third expression increases the value of x to 4;

4 < 5 is true, so4 anda new line are printed by the Trace statement. Next, the value of x increases to 5. 5 < 5 is not true, so the loop ends.

return Statements

Every function returns a value, which is usually designated in a return statement. A return statement returns the value of an expression to the calling environment. It uses the following form:

return <expression>;

An example of a return statement and its calling environment is

Trace ( HiThere() );
... 
HiThere()
{
  return "Hi there";
}

The call to the primitive function Trace causes the function HiThere() to be executed. HiThere() returns the string “Hi there” as its value. This value is passed to the calling environment (Trace), resulting in this output:

Hi there

A return statement also causes a function to stop executing. Any statements that come after the return statement are ignored, because return transfers control of the program back to the calling environment. As a result,

C++
Trace ( HiThere() );
... 
HiThere()
{
  a = "Hi there";
  return a;
  b = "Goodbye";
  return b;
}

will output only

Hi there

because when return a; is encountered, execution of the function terminates, and the second return statement (return b;) is never processed. However,

C++
Trace ( HiThere() );
... 
HiThere()
{
  a = "Hi there";
  b = "Goodbye";
  if ( 3 != 3 ) return a;
  else return b;
}

will output

Goodbye

because the if statement evaluates to false. This causes the first return statement to be skipped. The function continues executing with the else statement, thereby returning the value of b to be used as an argument to Trace.

Compound Statements

A compound statement, or statement block, is a group of one or more statements that is treated as a single statement. A compound statement is always enclosed in curly braces ( {} ). Each statement within the curly braces is followed by a semicolon; however, a semicolon is not used following the closing curly brace.

The syntax for a compound statement is

C++
{
  <first_statement>;
  <second_statement>;
  ...
  <last_statement>;
}

An example of a compound statement is

C++
{
  x = 2;
  x + 3;
}

It's also possible to nest compound statements, like so:

C++
{
  x = 2;
  {
    y = 3;
  }
  x + 3;
}

Compound statements can be used anywhere that any other kind of statement can be used.

if (3 && 3)
{
  result = "True!"; 
  Trace(result);
}

Compound statements are required for function declarations and are commonly used in if, if-else, while, and for statements.