Link Search Menu Expand Document

Control Flow

The control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.

Table of contents

  1. Introduction
  2. Branching with If/Else
  3. Looping with For
  4. Looping with While

Introduction

Since FLY is based on Java, it supports the three main control flow structures of Java, which are: if/else, while and for.

Each control structure has the main purpose of the Java corrispective, but it’s syntax can be different.

Note. In this guide we are not going to explain the purpose of each control flow structure since it should be already known by the reader.

Branching with If/Else

The main structure of the If/Else control flow structure is as the following:

if (expression) {
    statements
} else if (expression) {
    statements
} else {
    statements
}

The else if and the else branches are optional, so can be skipped.

The expressions inside the round brackets must return a Boolean value.

Looping with For

The for expression is specified by the following syntax:

for INDEX in OBJECT ?(by DELIMITER) {
    statements
}

This structure can be not familiar for Java users, that’s why we are going to explain all the components of that structure:

  1. INDEX: INDEX is just one or more variable which will assume the iteration value of the iterator.
  2. OBJECT: OBJECT is where the INDEX needs to iterate. OBJECT can be an array, a map, a dataframe, or a range object (later on we will explain the range object).
  3. DELIMITER: the DELIMITER can assume three different values, which are row, col or delimiter followed by a string. Essentially, a delimiter tells how the OBJECT must be iterated by the INDEX.

Range Object

A range object is just a simple way to iterate over a range of values. The syntax of a range object literal is the following [V1 : V2] where V1 and V2 can be Integers variable, constants or literals.

Examples

The following example shows how to iterate an houndred times over the index i:

for i in [0:100] {
    println i
}

The following example shows how to iterate over an array:

var arr = {1, 2, 3, 4, 5}

for i in arr {
    println i
}

The following example shows how to iterate over a dataframe given a certain delimiter:

var dat = [type="dataframe", ...]

for i in dat by row {
    println i
}

Looping with While

A while expression is defined by the following syntax:

while (condition) {
    statements
}

While expression doesn’t need examples since is simple as it seems.