Skip to content

Control flow

Julius Paffrath edited this page Jul 18, 2018 · 7 revisions

Control flow types

In jask you can use different types to control your program:

  • if-else conditions
  • run-loops (like for or while loops in other languages)

Conditions with if-else

See the following example how to use if-else conditions:

if 1 equals 1
    print("equal")
else
    print("not equal")
endif

If you don't need the else-block, you still have to specify it:

if 1 equals 1
    print("equal")
else
endif

You can use the following statements in your if-else condition:

  • equals
  • unequals
  • greater
  • greaterequals
  • smaller
  • smallerequals

Additionally, you can use the modulo operator:

if myNum mod 2 equals 0
    print(myNum:" is even!")
else
    print(myNum:" is odd!")
endif

Loops with run and while

If you want to execute parts of your code multiple times, use a run loop:

run i from 0 to 10 with i plus 1
    printLine("i is ":i)
endrun

A loop combined with a condition is called a while loop:

while CONDITION
    [...]
endrun

CONDITION can be everything that works in an if-condition. For example:

store 0 in i
while i smaller 100
    print(i)
    assign i times 2 to i
endrun

If you want to iterate a list, use the for loop:

for element in list(1:2:3)
    printLine(element)
endrun

Clone this wiki locally