Plof Conditionals
First things first -- if and else. If and else work pretty similarly to other languages, except that there are no curly braces, only parenthesis. The reason for this is that they're not statements built into the language, just functions, and the expressions to be evaluated conditionally are arguments. Here's an example of their use:
var num = 5
if (num == 5) (
Stdout.write "num == 5"
) else (
Stdout.write "num != 5"
)Note that parenthesis cannot come on the line after if() or else(), and else() must be on the same line as the end of the if() block. This is because if() and else() are functions, not built-in constructs, and a newline ends a statement, cutting the function call short. This example also shows a couple of comparison operators and some operators for NativeInteger. Here is a complete list or comparison operators:
"==" -- equal to
"!=" -- not equal to
">" -- greater than
"<" -- less than
">=" -- greater than or equal to
"<=" -- less than or equal to
"===" -- exactly equal to (the same object)
"!==" -- not exactly equal to (will be true even if the values of the objects are equal)
And here are the operators for NativeInteger:
"*" -- multiplication
"/" -- division
"+" -- addition
"-" -- subtraction
"%" -- modulus
For each of these NativeInteger operators there is a corresponding "assign-operate" operator -- for example, x += 5 is the same as x = x + 5 and x %= 5 is the same as x = x % 5. One thing to be careful of when working with === and !== is that the first 100 integers are cached and are thus exactly equal to each other, but other integers are not. Thus, 29 === 29 is true, but 101 === 101 is false.
Now for two more Plof control structures: while and for. while() works like this:
var i = 2
while (i < 50) (
if (i % 2 == 0) (
Stdout.write (i.toString() ~ "\n")
)
i++
)NativeInteger.toString(), by the way, does just what it sounds like it would do -- it converts a NativeInteger to a String. Here is how for() works:
var i
for (i = 2)(i < 50)(i++) (
if (i % 2 == 0) (
Stdout.write (i.toString() ~ "\n")
)
)Plof also allows the use of the "break" and "continue" functions inside loops. "break" exits the loop, and "continue" goes to the next iteration. For example:
var i
for (i = 2)(i < 50)(i++) (
if (i < 10) (
continue()
)
if (i > 40) (
break()
)
if (i % 2 == 0) (
Stdout.write (i.toString() ~ "\n")
)
)There is also a selection of collections available in Plof. The most commonly used one (and the only one with its own syntax) is ListArray. A ListArray is used like an array, but allows for fast concatenation, with the caveat that indexing becomes slower with every concatenation. Put differently, if you treat a ListArray like a list, operating on it will have the efficiency of a list, and if you treat it like an array, operating on it will have the efficiency of an array. A ListArray can be declared with the following syntax:
var la = [[ 1, 2, 3 ]]
ListArrays (and all collections) have an "each" function, allowing iteration over the elements:
var x
[[ "Hello, ", "world!\n" ]].each (ref x) (
Stdout.write x
)In the next tutorial, we'll take a look at Plof's object system.