Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Programming with ooc/Control

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Control flow __TOC__ if (condition) { statement } else { other statement } if (condition) { statement } else if (condition) { other statement } else if (condition) { other statement ... } else { 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: match (x) { case 1 => walkTheDog() case 5 => launchNuke() case => 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: match { case (x < 0) => -1 case (x > 0) => 1 case => 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 match (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 — like any ooc operator — be overloaded. This means that match works with strings as well, which have == overloaded in the core sdk: match (humor) { case "sad" => ohNoes() case "happy" => yay() case => bahHumbug() } By overloading the == operator, it is possible to use any user-defined type in a match statement. Unconditionals return return exits the current function, optionally passing to the outer scope any value you gave to it return 42 return doesn't always have a value, it can also only simply be used to exit a function at any point return 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 Edsger Dijkstra March 1968 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF PDF Go To Statement Considered Harmful Communications of the ACM 11 3 147–148 10.1145/362929.362947 The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.. while (true) { doSomething() } while (x < 5) { x = calculateSomething() } while (true) { x = calculateSomething() if (x > 5) { break } 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. while (true) { x = calculateSomething() if (x == 5) { skip the 5th step continue } doSomething(x) } for (i in 1..11) { doSomething(i) } Ranges are delimited by .. — You can store ranges and re-use them later, e.g.: range := 1..11 then, later: for (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<Int> for (state in list) { println(state) } See also Wikibook Programming with ooc References