<- ^ ->
Control structures

4   Control structures

Basic control structures include while, do ... while and for loops, if conditionals, and break and continue jump statements.

Gont control structures are in few places more restricted then these found in C. This is to satisfy safety paradigm. You might find this annoying, upon first reading.

4.1   Empty instruction

There is no empty instruction (written as `;' in C). skip keyword is introduced instead. For example:

C:
        while (f(b++));
Gont:
        while (f(b++))
                skip;

4.2   Conditions

Type of condition in control structures has to be bool. Therefore, C code:
        int i = 10;
        while (i--)
                f();
needs to be written as:
        int i = 10;
        while (i-- != 0)
                f();

4.3   Dangling else

In C else is associated with nearest else-free if. The grammar of C is said to be ambiguous because of this. This can be real problem sometimes, following code, due the indentation used, is misleading:
        if (foo)
                if (bar)
                        baz();
        else
                qux();
In Gont one needs to use { } when putting one if into another.

4.4   Labeled loops

In Gont, similarly to Ada or Perl, loops can be given names, in order to later on tell break and continue which exactly loop to break. This looks as:
        foo: while (cond) {
                bar: for (;;) {
                        if (c1)
                                break foo;      // break outer loop
                        else
                                continue bar;   // continue inner loop
                        for (;;) {
                                if (c2)
                                        break;  // break enclosing loop
                        }
                }
        }
<- ^ ->
Control structures