Statements

A statement can be one of the following:

Statements specify the flow of control as a User Language Program executes. In absence of specific control statements, statements are executed sequentially in the order of appearance in the ULP file.

Compound Statement

A compound statement, also known as block, is a list (possibly empty) of statements enclosed in matching braces ({ }). Syntactically, a block can be considered to be a single statement, but it also controls the scoping of identifiers. An identifier declared within a block has a scope starting at the point of declaration and ending at the closing brace.

Compound statements can be nested to any depth.

Expression Statement

An expression statement is any expression followed by a semicolon.

An expression statement is executed by evaluating the expression. All side effects of this evaluation are completed before the next statement is executed. Most expression statements are assignments or function calls.

A special case is the empty statement, consisting of only a semicolon. An empty statement does nothing, but it may be useful in situations where the ULP syntax expects a statement but your program does not need one.

Control Statements

Control statements are used to control the program flow.

Iteration statements are

do...while
for
while

Selection statements are

if...else
switch

Jump statements are

break
continue
return

break

The break statement has the general syntax

break;

and immediately terminates the nearest enclosing do...while, for, switch or while statement. This also applies to loop members of object types.

Since all of these statements can be intermixed and nested to any depth, take care to ensure that your break exits from the correct statement.

continue

The continue statement has the general syntax

continue;

and immediately transfers control to the test condition of the nearest enclosing do...while, while, or for statement, or to the increment expression of the nearest enclosing for statement.

Since all of these statements can be intermixed and nested to any depth, take care to ensure that your continue affects the correct statement.

do...while

The do...while statement has the general syntax

do statement while (condition);

and executes the statement until the condition expression becomes zero. The condition is tested after the first execution of statement, which means that the statement is always executed at least one time.

If there is no break or return inside the statement, the statement must affect the value of the condition, or condition itself must change during evaluation in order to avoid an endless loop.

Example

string s = "Trust no one!";
int i = -1;
do {
   ++i;
   } while (s[i]);

for

The for statement has the general syntax

for ([init]; [test]; [inc]) statement

and performs the following steps:

  1. If an initializing expression init is present, it is executed.
  2. If a test expression is present, it is executed. If the result is nonzero (or if there is no test expression at all), the statement is executed.
  3. If an inc expression is present, it is executed.
  4. Finally control returns to step 2.

If there is no break or return inside the statement, the inc expression (or the statement) must affect the value of the test expression, or test itself must change during evaluation in order to avoid an endless loop.

The initializing expression init normally initializes one or more loop counters. It may also define a new variable as a loop counter. The scope of such a variable is valid until the end of the block which encloses the for loop.

Example

string s = "Trust no one!";
int sum = 0;
for (int i = 0; s[i]; ++i)
    sum += s[i]; // sums up the characters in s

if...else

The if...else statement has the general syntax

if (expression)
   t_statement
[else
   f_statement]

The conditional expression is evaluated, and if its value is nonzero the t_statement is executed. Otherwise, the f_statement is executed in case there is an else clause.

An else clause is always matched to the last encountered if without an else. If this is not what you want, you need to use braces to group the statements, as in

if (a == 1) {
   if (b == 1)
      printf("a == 1 and b == 1\n");
   }
else
   printf("a != 1\n");

return

A function with a return type other than void must contain at least one return statement with the syntax

return expression;

where expression must evaluate to a type that is compatible with the function's return type. The value of expression is the value returned by the function.

If the function is of type void, a return statement without an expression can be used to return from the function call.

switch

The switch statement has the general syntax

switch (sw_exp) {
  case case_exp: case_statement
  ...
  [default: def_statement]
  }

and allows for the transfer of control to one of several case-labeled statements, depending on the value of sw_exp (which must be of integral type).

Any case_statement can be labeled by one or more case labels. The case_exp of each case label must evaluate to a constant integer which is unique within it's enclosing switch statement.

There can also be at most one default label.

After evaluating sw_exp, the case_exp are checked for a match. If a match is found, control passes to the case_statement with the matching case label. If no match is found and there is a default label, control passes to def_statement. Otherwise none of the statements in the switch is executed.

Program execution is not affected when case and default labels are encountered. Control simply passes through the labels to the following statement.

To stop execution at the end of a group of statements for a particular case, use the break statement.

Example

string s = "Hello World";
int vowels = 0, others = 0;
for (int i = 0; s[i]; ++i)
    switch (toupper(s[i])) {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U': ++vowels;
                break;
      default: ++others;
      }
printf("There are %d vowels in '%s'\n", vowels, s);

while

The while statement has the general syntax

while (condition) statement

and executes the statement as long as the condition expression is not zero.

The condition is tested before the first possible execution of statement, which means that the statement may never be executed if condition is initially zero.

If there is no break or return inside the statement, the statement must affect the value of the condition, or condition itself must change during evaluation in order to avoid an endless loop.

Example

string s = "Trust no one!";
int i = 0;
while (s[i])
      ++i;