User Tools

Site Tools


script:walkthrough:statements_and_compounds

Statements and compounds

Structure

Statements normally end at a newline character, unless there is a backslash at the end of a line.

print 'Statement'

print \
  'Statement'

Semicolons can be used on one line to separate statements.

print 'Statement 1.'; print 'Statement 2.'

Compounds are blocks of statements that end with the end keyword.

if True
    print 'Compound inside "if"'
end

As a special case, single-statement compounds can be created with a colon :.

if True: print 'No "end" needed here'

The keyword pass can be used in place of an empty compound.

if True: print "'Tis true."; else: pass

Each compound must be closed with its own end keyword.

if True
    if True
        if True
            if True
            end
        end
    end
end

Assignment

The assign statement is used to create new variables in the local namespace.

i = 10

Variables are untyped, so any other kind of value can later be assigned to the same variable.

i = "abc"

Arrays are modifiable, so one can change the value of an individual element.

$ array = [1, 2, 3, 4]  
  
$ array[1] = 100

$ print array
[ 1, 100, 3, 4 ]

Nested arrays can also be assigned into.

$ array[2] = ['a', 'b', 'c']

$ array[2][1] = 'Bee'

$ print array
[ 1, 100, [ a, Bee, c ], 4 ]

One can assign to dictionaries to add or modify the values of keys.

$ d = {1:2, 'Three':4}

$ d[5] = 'Five'

$ print d
{ 1: 2, 5: Five, Three: 4 }

When assigning, the right-hand value is copied to the new variable.

array = [1, 2, 3]
origArray = array
array[1] = 10
# origArray remains unmodified

Weak assignment

Weak assignment ?= only creates new variables. It never modifies existing ones.

$ print "z exists?", 'z' in locals()
z exists? False

$ z ?= 3

$ print "z exists?", 'z' in locals()
z exists? True

$ print 'z =', z
z = 3

$ z ?= 10    # this one does nothing

$ print 'z =', z
z = 3

const

You can use the const keyword when assigning to create a read-only variable.

$ const z = 10

$ print 'z has been consted to', z
z has been consted to 10

Attempting to assign a new value to a const variable causes an exception.

try
    z = 10
    print 'Allowed to reassign the same value...'
    z = 5
catch ReadOnlyError
    print 'Have to delete z before its value can change.'
end

Output:

Allowed to reassign the same value...
Have to delete z before its value can change.

del

Variables and functions can be deleted with the del keyword.

del z

Flow control

if, elsif, else

if, elsif, and else are used for conditional branching.

if False
    print 'a)'
elsif False
    print 'b)'
elsif True > False
    print 'c)'
else
    print 'd)'
end

while

i = 5
while i > 0
    print 'Still looping because %i > 0' % i
    i -= 1
end

Output:

Still looping because 5 > 0
Still looping because 4 > 0
Still looping because 3 > 0
Still looping because 2 > 0
Still looping because 1 > 0

break

The break statement is used to break out of loops.

while True
    print "I won't stay here."
    break
    print "Jumped over this."
end

Optionally, a number argument can be given to break to break out of multiple nested loops. Below, the break escapes from two loops.

while True
    while True
        print "Uh-oh, now I'm in trouble!"
        break 2
    end
end
print "Whew"

for

for loops iterate over a sequence of array elements, dictionary key-value pairs, or string characters. The loop variable is not deleted afterwards, so it can be checked if the loop was terminated early with break.

for i in ['planes', 'trains', 'automobiles']
    print i
end
print 'Value of i after the "for" loop is', i

Output:

planes
trains
automobiles
Value of i after the "for" loop is automobiles

When iterating a dictionary, the loop variable is an array containing the key and the value.

a = {'a': 1, 'b': 2}
for i in a
    print i
end

Output:

[ a, 1 ]
[ b, 2 ]

continue

The continue statement interrupts the loop and returns flow back to the beginning, with the next element.

for i in ['planes', 'trains', 'automobiles']
    if i == 'trains': continue
    print i, 'are cool!'
end

Output:

planes are cool!
automobiles are cool!
script/walkthrough/statements_and_compounds.txt · Last modified: 2019-11-20 17:30 by skyjake