Overview of control flow

Gosu has all the common control flow structures, including improvements on the Java versions.

if statements

Gosu has the familiar if, else if, and else statements:

if (myRecord.Open and myRecord.MyChildList.length > 10) {
  // Some logic
} else if (not myRecord.Open) {
  // Some more logic
} else {
  // Yet more logic
}

Gosu permits the more readable English words for the Boolean operators: and, or, and not. Alternatively, you can use the symbolic versions from Java (&&, ||, and !).

for loop

The for loop in Gosu is similar to the Java 1.5 syntax:

for (ad in addressList) {
  print(ad.ID)
}

This syntax accepts arrays or any Iterable object. Although the syntax does not specify a type for ad, the variable is strongly typed. Gosu infers the type based on the iterated variable’s type. In the previous example, if addressList has type Address[], then ad has type Address. If the addressList variable is null, Gosu skips the for statement entirely, and generates no error. In contrast, Java throws a null pointer exception if the iterable object is null.

If you need an index within the loop, use the following syntax to access the zero-based index:

for (ad in addressList index i) {
  print(ad.ID + " has index " + i)
}

Intervals

Gosu has native support for intervals. An interval is a sequence of values of the same type between a given pair of endpoint values. For example, the set of integers beginning with 0 and ending with 10 is an integer interval. A closed interval contains the starting and ending values, in this example, including 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. The Gosu syntax for this closed interval is 0..10. A typical use for intervals is in writing concise for loops:

for (i in 1..10) {
  print(i)
}

An open interval excludes the value at one or both ends of the interval. The Gosu syntax 1|..|10 defines an interval that is open on both sides, and contains the values from 2 through 9.

Intervals do not need to represent numbers. Intervals can be of types including numbers, dates, or other abstractions such as names. Gosu accepts the shorthand syntax of the two periods, (..) for intervals of dates and common number types. You can also add custom interval types that support iterable comparable sequences. If your interval type implements the required interfaces, you can use intervals of that type in for loop declarations:

for (i in new ColorInterval("red", "blue")) {
  print(i)
}

Gosu does not have a direct general purpose equivalent of the Java three-part for declaration:

for (i =1 ; i <20 ; ++i)

In practice, using intervals makes most use of this pattern unnecessary. If necessary, you can use a Gosu while loop to duplicate this pattern.

To use intervals with for loops, the interval must be iterable. Custom non-iterable intervals are useful mainly for math and theoretical work. For example, a non-iterable interval could represent non-countable values such as the infinite set of real numbers between two other real numbers.

switch statement

The Gosu switch statement can test any type of object, with a special default case at the end:

var x = "b"

switch (x) {
  case "a":
    print("a")
    break
  case "b":
    print("b")
    break
  default:
    print("c")
}

In Gosu, you must put a break statement at the end of each case to jump to the end of the switch statement. Otherwise, Gosu continues to the next case in the series. In the previous example, if you remove the break statements, the code prints both "b" and "c". Java has the same requirement. Some other languages do not require the break statement to prevent continuing to the next case.

See also