else Keyword

The else keyword is an optional part of if-else statement.

if, else Statement

if( expression )
statement1
[else
statement2]

The if keyword executes statement1 if expression is true (nonzero); if else is present and expression is false (zero), it executes statement2. After executing statement1 or statement2, control passes to the next statement.

Example 1

if ( i > 0 )
    y = x / i;
else 
{
    x = i;
    y = abs( x );
}

In this example, the statement y = x/i; is executed if i is greater than 0. If i is less than or equal to 0, i is assigned to x and abs( x ) is assigned to y. Note that the statement forming the if clause ends with a semicolon.

When nesting if statements and else clauses, use braces to group the statements and clauses into compound statements that clarify your intent. If no braces are present, the compiler resolves ambiguities by associating each else with the closest if that lacks an else.

Example 2

if ( i > 0 )           /* Without braces */
    if ( j > i )
        x = j;
    else
        x = i;

The else clause is associated with the inner if statement in this example. If i is less than or equal to 0, no value is assigned to x.

Example 3

if ( i > 0 ) 
{             /* With braces */
    if ( j > i )
        x = j;
}
else
    x = i;

The braces surrounding the inner if statement in this example make the else clause part of the outer if statement. If i is less than or equal to 0, i is assigned to x.

Common misunderstandings

"New if-else problem"

Question:

Why I get the syntax error when I write: if( H > Ref(H,-1) )

Answer:

if-else statement changes flow of execution (opposite to IIF function that evaluates all arguments and works on arrays) and you can not really write

if ( H >Ref(H,-1) )

because it has no meaning. It would translate to " If high array is higher than high array shifted one bar" (see tutorial below). Flow control statement (such as if-else) has to get SINGLE boolean value to make decision which execution path should be taken. If you write H (or High) it means ARRAY (entire array).
if you write H[ i ] - it means i-th element of the array. The subscript operator [ ] allows you to access individual array elements.

Instead you should write:

for( i = 1; i < BarCount; i++ )
{
   
if ( High[ i ] > High[ i - 1 ] )
   {
       x[ i ] =
High[ i ];
   }
   
else
   {
       x[ i ] =
Low[ i ];
   }
}



this will translate to correct one "for EVERY BAR 'i' assign i-th element of high array to the i-th element of x array if i-th element of high array is higher than the previous element, otherwise assign i-th of low array to the i-th element of x array". The rule is: new if-else and while statements need single boolean value (not array) to decide which execution path should be taken. If you want to use them with arrays you have to iterate through bars using for loop (as shown above).

On the other hand this can be implemented in single line using old-style array operations and IIF function:

x = IIf( High > Ref( High, -1 ), High, Low );

This works because IIF operates on ARRAYS as described in the tutorial.

As you can see in many cases old-style AFL provides much more compact form. I always tried to explain this advantage of AFL but only a few realised that. New control statements should be used where it is better to use them. As I tried to explain during last years in 80% of cases 'old-style' AFL provides the shortest formula. Only remaining 20% of cases needed script. Those 'script-only' cases now can be coded in native AFL thanks to new for/while/if-else statements. And this is correct usage of them - to replace script parts.