Variables
Definition
Use the syntax var identifier = value
(see identifiers and values) to assign the value value
to the variable named identifier
. The expression var identifier = value
returns value
.
To assign the same value to multiple variables, you can use var variable1 = var variable2 = var variable3 = value
.
If you want to assign multiple values to multiple variables, you can use var variable1, variable2 = value1, value2
. This can be used to swap variables: var a, b = b, a
.
Except for keywords, there are no prohibited variable name.
Edition
You can edit variables by multiple ways:
var a = value
;var a += value
: same asvar a = a + value
;var a ++
: same asvar a += 1
;var a -= value
: same asvar a = a - value
;var a --
: same asvar a -= 1
;var a *= value
: same asvar a = a * value
;var a /= value
: same asvar a = a / value
;var a ^= value
: same asvar a = a ^ value
;var a //= value
: same asvar a = a // value
;var a %= value
: same asvar a = a % value
;var a ||= value
: same asvar a = a or value
;var a &&= value
: same asvar a = a and value
;var a ^^^= value
: same asvar a = a xor value
;var a |= value
: same asvar a = a | value
;var a &= value
: same asvar a = a & value
;var a ^^= value
: same asvar a = a ^^ value
;var a === value
: same asvar a = a == value
;var a <== value
: same asvar a = a <= value
;var a <<= value
: same asvar a = a < value
;var a >== value
: same asvar a = a >= value
;var a >>= value
: same asvar a = a > value
.
Info
For details about these operators, see this page.
Note
You can also edit multiple variables at the same time:
var a, b += 1, 2
: same asvar a += 1 ; var b += 2
var a, b ++
: same asvar a, b += 1, 1
var a, b //= b, a
: same asvar c = a ; var a //= b ; var b //= c ; del c
Access
To access to a variable, you can:
- give the identifier:
foo
(returns the value offoo
) - use the special syntax:
foo ? bar ? a ? b
(returns the value offoo
if it is defined, otherwise the value ofbar
... You can put as much? identifier
as you want.) Then, you can put an expression at the end, such as2
. In this case, the value returned will be2
if none of the given identifiers is defined.
Deletion
If you don't need a variable anymore, you can delete it with the del
keyword: del identifier
.
Examples
var foo = var bar = 12
while foo != bar - 1 then var bar -= 1
(see while loop)
Example for deletion
Create a variable a
, the delete it:
nougaro> var a = 1
1
nougaro> del a
But be careful! It can return errors:
nougaro> var a = 1
1
nougaro> del a
nougaro> del a
Traceback (more recent call last):
In file <stdin>, line 1, in <program>:
del a
^
NotDefinedError: a is not defined.