Booleans
Boolean Literal Syntax
There are two boolean literals: true, which represents the boolean value true, and false, which represents the boolean value false.
When Automatic Conversion to a Number , true yields 1 and false yields 0.
Automatic Conversion to a Boolean
When a function, method or statement which expects a boolean as one of its arguments is passed a non-boolean value, this value is automatically converted to a boolean as follows:
-
The Numbers 0 yields false;
-
The empty Strings "" yields false;
-
The The null Value value yields false;
-
The The undefined Value value yields false;
-
Any other non-boolean values yield true.
For example:
if ("") writeln("True"); else writeln("False");
if (123) writeln("True"); else writeln("False");
displays "False", then "True".
Boolean methods
The only boolean method is:
Syntax |
Effect |
Returns a string representing the boolean, either "true" or "false". Example: true.toString -> "true" false.toString -> "false" |
Logical Operators
The following boolean operators are available:
Note
For C/C++ programmers: These operators are the same as in C and C++. |
Syntax |
Effect |
! boolean |
Logical negation. Examples: ! true -> false ! false -> true |
exp1 && exp2 |
Returns true if both boolean expressions exp1 and exp2 are true. Otherwise, returns false. If exp1 is false, this expression immediately returns false without evaluating exp2, so any side effects of exp2 are not taken into account. Examples: true && true -> true true && false -> false false && whatever-> false; whatever is not evaluated. |
exp1 || exp2 |
Returns true if either boolean expression exp1 or exp2 is true. Otherwise, returns false. If exp1 is true, this expression immediately returns true without evaluating exp2, so any side effects of exp2 are not taken into account. Examples: false || true -> true false || false -> false true || whatever-> true; whatever is not evaluated. |
condition ? exp1 : exp2 |
If condition is true, this expression returns exp1; otherwise, it returns exp2. When condition is true, the expression exp2 is not evaluated, so any side effects it may contain are not taken into account. Similarly, when condition is false, exp1 is not evaluated. Examples: true ? 3.14 : whatever-> 3.14 false ? whatever : "Hello"-> "Hello" |