Short-circuit Evaluation

An IF statement is short-circuited (independent of optimization) if the test expression contains a either logical OR (|) or a logical AND (&) of at least two expressions, where the result type of each expression is bit(1) (TRUE or FALSE). This can apply recursively to each left and right operand expression.

If the test expression is a logical OR, and the left-hand side expression is TRUE, then the right-hand side expression is not evaluated. Conversely, if the test expression is a logical AND, and the left-hand side expression is FALSE, then the right-hand side expression is not evaluated.

Examples

dcl i fixed bin (31) static init(1);
dcl j fixed bin (31) static init(2);
dcl b3 bit(3) static init ('010');
dcl p pointer static init (null());
dcl fb31 fixed bin (31) based;
    if (i = 1) | (j > 2) then    /* (j>2) is not evaluated */
        … ;
    if (i ^= 3) & (j > 2) then  /* (j>2) is not evaluated */
        … ;
    if ((i = 1) | (j > 2)) & ((i ^= 0)|( j =2)) then /* (j>2) and (j=2) is not evaluated */
        … ;
    if substr(s3,2,1) | substr(b3,1,1) then  /* substr(b3,1,1) is not evaluated */
        … ;
    if (p ^= null() & p->fb31 = 0) then      /* p->fb31 = 0 is not evaluated  */
        …;
    if ^(i = 1) | (j > 2)) then     /* -opt required for short-circuiting, test expression is NOT */
       … ;