Skip to content

If & else

Giorgio Garofalo edited this page Jun 16, 2022 · 7 revisions

Pikt handles conditions the traditional way via if / else / else if statements. Syntax:

if <condition> <block>
else if <condition> <block>
else <block>

Where <condition> can be any kind of expression. If it is a boolean expression, the condition is true only if the boolean is true. Otherwise the condition is true if the given expression is not null, but we will learn about nullable values later.

<block> contains the code to be executed if the condition is satisfied.
It can be either a single action, such as a print, a function call or a variable update, or a more complex block. In that case, it has to be surrounded by lambda.open and lambda.close , as shown in the for-each guide.

Example

Pseudocode:

if (true) {
    print("OK")
} else {
    print("NO")
}

Pikt (remember that true is represented by bool.true ):

But, since print is a single action, we can remove lambda.open and lambda.close in order to make it look more compact:

Logical and relational operators

Pikt offers a standard set of logical and relational operators to build boolean expressions:

  • op.and (&&)
  • op.or (||)
  • op.equality (==)
  • op.inequality (!=)
  • op.greater (>)
  • op.greater_or_equals (>=)
  • op.less (<)
  • op.less_or_equals (>=)

Like many programming languages, operators must be put between two values.

Example

Pseudocode:

var my_var = 1
if (my_var == 1) {
    print("OK")
} else if (true && false) {
    print("NO")
}

Pikt (remember that false is represented by bool.false ):