if (boolean) {The else part is optional.
do something
...
} else {
do something else
...
}
boolean ? do this : do that
if (a == 1) {Note that nesting conditional statements is not allowed. Therefore, the following code creates a syntax error:
print "A is one"
} else {
print "A is not one but " + a
}The same thing in one line:
print "A is " + (a == 1 ? "one" : "not one but " + a)
if (a == 1) {If you want to do something like this, do the following:
print "one"
} else if (a == 2) { <== THIS IS NOT ALLOWED
print "two"
} else {
print "neither one or two"
}
if (a == 1) {Or, as a (complicated) one-liner, since nesting is allowed using the second construct:
print "one"
} else {
if (a == 2) {
print "two"
} else {
print "neither one or two"
}
}
print (a == 1 ? "one" : (a == 2 ? "two" : "neither one or two"))
(You can leave out all the parentesis, but for readibility it is better to use them)
for (start_condition; boolean; change_condition) {A while loop is constructed like this:
do something
...
}
while (condition) {
do something
...
}
for (i = 1; i < 10; i++) print iNote that in the second example, it is easy to create an endless loop if you forget the i++ statement. You can always use curly brackets to execute more than one statement in a loop or as a result of a conditional statement.i = 1
while (i < 10) {
print i
i++
}
Assign the ASCII character set to a string variable:
s = ""
for (i = 32; i <= 255; i++) s += char(i)