Najova

DocsGuides

Blocks

A block runs the lines between its first line and its end line. Blocks make choices and repeat things.

Choose with $if

$if runs its lines when its condition is true. Add $elseif and $else for other cases, and end with $endif.

fizzbuzz.naja
$loop[n;1;15]
  $if[$mod[$get[n];15] == 0]
    $print[FizzBuzz]
  $elseif[$mod[$get[n];3] == 0]
    $print[Fizz]
  $elseif[$mod[$get[n];5] == 0]
    $print[Buzz]
  $else
    $print[$get[n]]
  $endif
$endloop
najova run fizzbuzz.naja
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Compare with ==, !=, <, >, <= and >=. Join conditions with && (and) and || (or), and turn one round with !.

Repeat

  • $loop counts: $loop[i;1;10], or $loop[i;10;0;-2] with a step.
  • $each goes through a list, or through the keys and values of a table.
  • $while repeats while a condition is true.
stock.naja
$let[stock;$table[apples;4;pears;0;plums;9]]
$each[fruit;amount;$get[stock]]
  $if[$get[amount] == 0]
    $continue
  $endif
  $print[$get[fruit]: $get[amount]]
$endeach
najova run stock.naja
apples: 4
plums: 9
$continue skips to the next round. $break leaves the block.