Conditionals

ASnake Documentation

Table Of Contents

Summary:

Means to check a condition which if True will execute a indented block of code. Conditionals can be chained together.

Syntax if:

An if checks if a expression is True, and if so, executes the code block within it. If False, the code block does not execute. The if's code block should be indented, or at least will imply an indent.

if 4 < 10
    print 'less than'
# result: less than

if 4 > 10
    print 'greater than'
# no result

Syntax else:

An else must be after a if, though outside of its block, unindented. It will spawn it's own code block which will execute if the preceding if or elif's result in False.

An else does not test for anything specific, it only triggers when the prior chained conditionals fail. Thus when using an else, it must be the last conditional block in a chain.

Example:

if 4 > 10
    print 'greater than'
else
    print 'less than'
# result: less than

Syntax elif:

elif stands for 'else if'. However it's behavior is a bit different, as it can be chained as many times as you need. Just like if, the condition must come after, and it contains a code block which runs if True. An elif when used must come after a if, and before a else.

Example:

if 4 > 10
    print 'greater than 10'
elif 4 > 3
    print 'greater than 3'
else
    print 'less than'
# result: greater than 3

Syntax match of:

Sometimes you have a giant chain of elifs testing against a specific variable, and it is too much boilerplate. ASnake provides a shortcut to get around that.

On it's own line, start with a match and then the value or variable you wish to test. Then you can chain as many ofs as you need, assuming that in it's place is an elif with the value you set. Note that it always defaults to an equality check, but you can specify another operator.

The chain can have an else at the end, it is optional.

Example:

myNumber = 4
match myNumber
of > 10 do 'greater than 10'
of 4 do 'its 4'
of 3 do 'its 3'
else 'less than'
# result: its 4

Syntax match case:

Python's case works similarly to ASnake's of, however is more complex. It uses pattern matching, and has a lot of features I don't care to describe here. Read about it in Python's documentation if you are interested.

Syntax Ternary:

Ternary is a binary decider within an expression. Useful for assignment when code blocks are unnecessary.

There are two ways to do it, Python's method and ASnake's method.

Python has the value which is applied if the conditional is True, then it has if and then the proceeding conditional. After that it has an else with the value after which will be assigned if the condition is False.

Example:

myVar = 'less' if 3 < 10 else 'greater'
print myVar
# result: less

myVar = 'less' if 3 > 10 else 'greater'
print myVar
# result: greater

ASnake's method is similar, except that it starts with the if and the condition. To mark the end of the condition there is an then or do, and the value to be set if the condition is True. After that it has an else with the value after which will be assigned if the condition is False, just like Python's method.

Example:

myVar = if 3 < 10 then 'less' else 'greater'
print myVar
# result: less

myVar = if 3 > 10 then 'less' else 'greater'
print myVar
# result: greater
Scroll to top