genie-logo

Decisions and loops

Page updated October 27, 2009
There is not yet an official logo for Genie/Vala, above image just a placeholder.

Introduction

We have all the usual candidates:

for, while, do, if, case

So let's get straight into examples. I particularly like the power of the for statement, so starting with that...

The for loop

Simple example:
init
for i:int = 1 to 10
print("%d",i)
Not much more to be said about that example!

Note though, Genie uses indentation for blocks of code, just like Python, but note one difference from Python -- no colon on the end of the for line. This means that if you do want to put the print statement on the sameline as the for condition, you have to do it like this, using the do keyword:
	for i:int = 1 to 10 do print("%d",i)
i is incremented from 1 up to 10, in increments of 1. But that to keyword does have a special meaning -- it means to count up. If you want to count down, you have to do it like this:
	for i:int = 10 downto 0 do print("%d",i)
Now for something I really like, the for loop can iterate through "iterable collection" datatypes...

Iterable collection data types

These are arrays and lists. A simple example, iterating through an array:
init
a:array of string = {"abc", "def", "xyz"}
for s in a
print(s)
One important point here: I didn't have to declare the data type of s as it is already inferred from a, that is, it is a string. The Genie compiler is not dumb!

There are also various ways of iterating through a string, for example, split the words into an array:
init
s:string = "The quick brown fox"
for s2 in s.split(" ",4) //last param =0, split all of string.
print(s2)
The split() function returns an array of strings, having split the string by the space character delimiter and with a limit of 4 fields. Once again, no need to specify the type of s2.

To iterate through a dictionary though, you can't just have for s in d where d is a dictionary -- you need to specify which half of the dictionary you want to iterate through, d.keys or d.values:
init
var d = new dict of string,string
d["First name"]="Barry"
d["Town"]="Perenjori"
d["State"]="WA"
for s in d.keys //note: older version of gee needs d.get_keys()
print("key=%s value=%s",s,d[s])

The while loop

We have two forms, while and do ... while. The former will loop through zero or more times, whereas the latter will loop through one or more times. Examples:
init
i:int = 0
while i < 10
i += 1
print("%d",i)
init
i:int = 0
do
i += 1
print("%d",i)
while i < 10

This naturally leads us to ask about the conditional operators, like the < less-than operator shown in the above examples. It's mostly the same as for C:

Relational operators

<
less than
a<b
>
greater than
a>b
<=
less than or equal to
a<=b
>=
greater than or equal to
a>=b
==
equal
a==b
!=
not equal
a!=b
is
equal
a is b
is not
not equal
a is not b

Logical operators

and
both are true
(a>2) and (a<10)
or
either are true
(a>2) or (b>5)
not
invert
not(b)

Sometimes we might want to break out of the loop...

The break and continue keywords

init
i:int = 0
while i<10
i += 1
if i == 5 do break
if i == 3 do continue
print("%d",i)
The break keyword is to break totally out of a loop, the continue keyword is to jump back to the start of the loop -- and in this case re-evaluate the while condition.

Unfortunately Vala/Genie does not support parameters for break and continue, so you can't do a one-step break out of nested loops.

The if decision

This is straight forward, but note that else if is named differently from Python which uses elif. An example:
init
i:int = 0
if i == 1
print("the value is 1")
else if i == 0
print("the value is 0")
else
print("fall through")

The case decision

This is also very straightforward, but note that unlike in C, there is no fall-through to the next case -- the first case that evaluates to true is the only one that gets executed. An example:
init
a:int = 3
case a
when 0,1,2
print "a is less than 3"
when 3
print "a is 3"
default
print "a is greater then 3"






(c) Copyright Nov. 2008,2009 Barry Kauler puppylinux.com, all reproduction rights reserved.