### Statements

The statement is the smallest executable unit in the syntax. A SQL procedure is a sequence of statements. Statements are separated by semicolon (;). There are many kinds of statements, such as declare statement, expression statement, assignment statement, if-else statement, and return statement.

#### Declare statement

The declare statement is used to declare variables.

- Syntax: `VariableType VariableName`

  Example: `String sql;`

  This statement declares a local String type variable called sql.

- Syntax: `global VariableType VariableName`

  Example: `global integer i;`

  This statement declares a global Integer type variable called i.

#### Expression statement

The expression statement is used to calculate the value of an expression.

#### Assignment statement

The assignment statement is used to calculate the expression on the right side of assignment operator and assign the value to the variable on the left side.

Syntax: `Variable = Expression`

The type of value of expression must be compatible with the type of the variable.

Example: `total = amount * price`

In this example, where the total, amount and price are declared variable. The statement calculate the "amount * price" and assign the value to "total".

#### If - else statement

The if-else statement provides conditional control of an action. Normally, the statements are executed one by one sequentially. The if-else statement enables alternative actions that depend on the evaluation result of the expression.

Syntax:

    if (expression)
    {
    statement(s)
    }
    or
    if (expression)
    {
    statement(s)
    }
    else
    {
    statement(s)
    }

Where, the expression must be a logical expression and have a Boolean value. The if clause checks the condition presented by the logical expression. If the condition is true, the statement following the if will be executed. Otherwise, the statement following the else will be executed.

Example:

    If (score >= 60)
    {
    return "PASS";
    }
    else
    {
    return "FAIL";
    }

In this example, if the score is greater than or equal to 60, the result is pass, otherwise the result is fail.

#### Return statement

The return statement is used to stop the execution of procedure and return a value if necessary. The return statement is the last statement executed.

Syntax: `return or return expression;`

The expression will be calculated before the procedure returns.

Example: `return results;`

#### For statement

The for statement provides a compact way to iterate over a range of values.

Syntax:

`for (initialization; termination; increment) {
statement
}`

Or

`for (initialization; termination; increment) statement`

The `initialization` is an expression that initializes the loop - it's executed once at the beginning of the loop. The `termination` expression determines when to terminate the loop. This expression is evaluated at the top of each iteration of the loop. When the expression evaluates to false, the loop terminates. Finally, `increment` is an expression that gets invoked after each iteration through the loop. All these components are optional.

Example:

    integer i=0, j=0;
    For(i=0;i<100;i=i+1){
    j=j+1;
    };
    return j;
    Or
    integer i=0, j=0;
    For(i=0;i<100;i=i+1) j=j+1;
    return j;

Both examples will return 100 as the result.

#### While statement

The while statement is used to continually execute a block of statements while a condition remains true.

Syntax:

`while (expression) do {
statement
}`

Or

`while(expression) do statement`

First, the while statement evaluates `expression`, which must return a Boolean value. If the expression returns true, then the while statement executes the `statement(s)` associated with it. The while statement continues testing the expression and executing its block until the expression returns false.

Example:

    integer i=0;
    While(i<100) do {
    i=i+1;
    };
    return i;

    Or
    integer i=0;
    While(i<100) do i=i+1;
    return i;

Both examples will return 100 as the result.

Note: JReport Designer provides another statement that is similar to the while statement - the do-while statement.

syntax:

`do { statement(s) } while (expression);`

Or

`do statement while(expression)`

Instead of evaluating the expression at the top of the loop, do-while evaluates the expression at the bottom. Thus the statements associated with a do-while are executed at least once.

Example:

    integer j=0;
    Do {
    j=j+1;
    }while(j<100);
    return j;
    Or
    integer j=0;
    Do j=j+1 while(j<100);
    return j;

Both examples will return 100 as the result.

#### Select statement

The select statement is usually used in the case that the value of a single variable may determine one of a number of different choices.

A select statement is given a variable and compares its value to all cases in the switch; if there is a case that matches the value, all statements in the matching case are executed. If none match, and a default is given, all statements following the default keyword are executed.

Syntax:

`select (variable name){
case expression_1: statement
case expression_2: statement
...
default: statement
};`

The `expression_1` and `expression_2` should be variables or constants. The statement in each should be a single statement or multi-statement (compound statement). When multi-statement is used, they must be enclosed by {}.

Notes:

- The value of the select is only evaluated one time even when a case modifies the tested value.
- No break statement is required, the logic flow does not continue through the following case statements.

Example:

    integer i = 0 ; string j = 'a';
    i = @"Customers_Customer ID";
    select(i)
    {
    case 1: j='bb'
    case 2: j='cc'
    default: j ='dd'};
    return j