3 Control structures
3.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;
This is to disallow common C's mistake:
while (cond);
do_sth;
(note `;' after while).
3.2 Control structures
-
if
cond stmt
-
if
cond block else
block
-
while
cond stmt
-
do
stmt while
cond
cond is expression of type bool. (therefore
int i=10; while (i--) f();
is not allowed, one needs to write
while (i-- != 0)
). stmt might be single statement or
a block. block is zero or more stmt's surrounded
with { }
. One might note that if's version with else
keyword is required to have { }
around each part. This is to
solve dangling-else problem, for example:
C:
if (b1)
if (b2)
f1();
else
f2();
This if of course parsed as:
if (b1) {
if (b2) {
f1();
} else {
f2();
}
}
In C else
is associated with nearest else-free if
.
As you have seen this can be source of problems.
However, as there is no danger with using if
without { }
and
else
, (as in if (x == 0) x++;
), it can be used without
{ }
. It is also allowed to have if (x == 0) x++; else x--;
,
as there are no if
in x--
nor x++
.
3.3 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
}
}
}