Increments

ASnake Documentation

Table Of Contents

Summary:

Increments and decrement operators to add or subtract before or after the statement.

Syntax ++i i-- :

Any reference or variable containing a int or float can have the ++ or -- operator next to it. ++ will increment, -- will increment.

[for now, bare variables have the best support]

x = 1
x++  # x is 2
x+=1 # x is 3
++x  # x is 4  
--x  # x is 3  
x--  # x is 2
x    # output: 2

From here on, when referring to a ++ or increment you can also assume it applies to -- decrement.

For a bare statement, the position of the increment doesn't change its behavior.

When placed inside a conditional, a ++x will increment it in place. A x++ will only increment if the conditional passes.

Inside conditionals:

x=1  
if x++ > 2 do x
# ^ condition will not be met as x is equal to 1.
if ++x < 2 do x
# ^ output: 2

When placed inside a while loop, the behavior changes. The variable will increment at the start of the loop if ++x, whereas x++ will increment at the end of the loop.

x=0
while x++ < 3
    print x
# output: 0 1 2
x=0
while ++x < 3
    print x
# output: 1 2 3
Scroll to top