Conditionals

If Statements

Two forms:

if (condition)
  <statement>

if (condition)
  <statement>
else
  <statement>

The condition must be Boolean. Only the taken branch executes.

Source: Statements

Example

pragma language_version >= 0.16;

import CompactStandardLibrary;

export ledger status: Uint<8>;

export circuit updateStatus(value: Uint<8>): [] {
    if (disclose(value) > (10 as Uint<8>)) {
        status = 1 as Uint<8>;
    } else {
        status = 0 as Uint<8>;
    }
}

Conditional Expressions

Compact has a ternary conditional expression:

e0 ? e1 : e2
  • e0 must be Boolean
  • e1 and e2 must have compatible types
  • Only the selected branch is evaluated

Source: Conditional expressions

Example

pragma language_version >= 0.16;

import CompactStandardLibrary;

export pure circuit max(a: Uint<64>, b: Uint<64>): Uint<64> {
    return a > b ? a : b;
}

export pure circuit clamp(value: Uint<64>, minVal: Uint<64>, maxVal: Uint<64>): Uint<64> {
    return value < minVal ? minVal : (value > maxVal ? maxVal : value);
}

Return

Every control-flow path must end in a return, unless the return type is [].

// For [] return type
return;

// For other return types
return <expr>;

Source: Circuits

Next Steps