Programming with ooc/Control

(condition) { statement } {     other statement }

(condition) { statement }  (condition) { other statement }  (condition) { other statement ... } {     another statement }

match
Often it is necessary to compare one specific variable against several constant expressions. For this kind of conditional expression the match statement exists. For example:

(x) { 1 =>     walkTheDog 5 =>     launchNuke =>     selfDestruct }

The last case here has an empty condition. It's called a catch-all case, which code will be executed in case none of the above cases are matched.

Unlike switch in C or Java, there is no need to break of a case explicitly with the break keyword. Only one case per match is only ever executed. This enforces simple designs, at the cost of some flexibility. For example, Duff's device can't be written in ooc.

However, in some ways, match is more flexible than a switch. For example, you can have a match without an initial condition:

{   (x < 0) => -1   (x > 0) => 1   =>      0 }

Here we define custom conditions inside the cases, rather than values to compare. The parenthesis are optional, but included here for the sake of readability. It behaves exactly as if we had written  (true) { on the first line.

Another difference with traditional switch statements is that a match can be used as an expression. In that case, it's evaluated to the value of the last statement in the case executed. It's a good idea to have a catch-all case when you use a match as an expression, so that there are no cases where the value is undetermined.

Finally, values are compared using the equality operator ==, which can &mdash; like any ooc operator &mdash; be overloaded. This means that match works with strings as well, which have == overloaded in the core sdk:

(humor) { "sad" => ohNoes "happy" => yay =>     bahHumbug }

By overloading the == operator, it is possible to use any user-defined type in a match statement.

return
return exits the current function, optionally passing to the outer scope any value you gave to it

42

return doesn't always have a value, it can also only simply be used to exit a function at any point

goto
ooc doesn't have a goto keyword. You can control the flow of loops with break and continue, and exit function via return. The ooc language thrives to be simple, and considers that goto doesn't belong to a high-level language. See Edsger Dijkstra's work for further reading.

(true) { doSomething }

(x < 5) { x = calculateSomething }

(true) { x = calculateSomething (x > 5) { }  doSomething(x) }

Skipping iterations
The continue keyword can be used to zap the rest of the loop's body and go directly to the next iteration.

(true) { x = calculateSomething (x == 5) { }  doSomething(x) }

(i in 1..11) { doSomething(i) }

Ranges are delimited by .. &mdash; You can store ranges and re-use them later, e.g.:

range := 1..11

(i in range) { doSomething(i) }

This works because 1..11 constructs a new Range, which is a cover type. Covers are explained in later chapters.

for allows to iterate through values of any types, as long as they implement the iterator method, returning an Iterator. For example, you can iterate over ArrayLists:

list := ["moody", "jolly", "bored"] as ArrayList (state in list) { println(state) }

Wikibook

 * Programming with ooc