Wiki
Version 2 (modified by Chuck, 15 years ago)

--

Expression Tour

This is a "tour" of various kinds of expressions in Cobra. It is intended to help you learn Cobra by quickly highlighting some interesting expressions you can write. It is neither a reference nor an exhaustive list of features. It assumes that the reader has basic familiarity with programming.

Displaying Expression Values

One of the best ways is to display an expression's value is to use the Trace statement which will show the source code of the expression, its value and the location of the trace in the source code (filename, line number, etc.):

trace p.x, p.y
    trace: p.x=5; p.y=6; at Foo.cobra:6; in Foo.main

You can, of course, use a Print statement. On a related note, you can validate the value of an expression with an Assert statement.

print 'My name is', name
assert x > 0

Arithmetic

Arithmetic has the usual infix operators and parentheses for grouping. Note that division with a single slash (/) gives a fractional value. Use a double slash (//) for "integer" division.

assert 4 / 5 = 0.8
assert 4 // 5 == 0
assert 6 / 5 == 1.2
assert 6 // 5 == 1

Augmented assignment operators can be used to change values:

i += 1
p.x *= 2

Underscores can be put in numbers to separate groups of digits for easier reading:

big = 10_000_000