This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| script:walkthrough:statements_and_compounds [2017-03-20 20:25] – skyjake | script:walkthrough:statements_and_compounds [2019-11-20 17:30] (current) – [Assignment] skyjake | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <- Operator expressions ^ ^ Exceptions -> | ||
| + | |||
| + | ====== Statements and compounds ====== | ||
| + | |||
| + | ===== Structure ===== | ||
| + | |||
| + | Statements normally end at a newline character, unless there is a backslash at the end of a line. | ||
| + | |||
| + | print ' | ||
| + | | ||
| + | print \ | ||
| + | ' | ||
| + | |||
| + | Semicolons can be used on one line to separate | ||
| + | |||
| + | print ' | ||
| + | |||
| + | Compounds are blocks of statements that end with the '' | ||
| + | |||
| + | if True | ||
| + | print ' | ||
| + | end | ||
| + | |||
| + | As a special case, single-statement compounds can be created with a colon '':'' | ||
| + | |||
| + | if True: print 'No " | ||
| + | |||
| + | The keyword '' | ||
| + | |||
| + | if True: print "' | ||
| + | |||
| + | Each compound must be closed with its own '' | ||
| + | |||
| + | 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 = " | ||
| + | |||
| + | 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] = [' | ||
| + | | ||
| + | $ array[2][1] = ' | ||
| + | | ||
| + | $ print array | ||
| + | [ 1, 100, [ a, Bee, c ], 4 ] | ||
| + | |||
| + | One can assign to dictionaries to add or modify the values of keys. | ||
| + | | ||
| + | $ d = {1:2, ' | ||
| + | | ||
| + | $ d[5] = ' | ||
| + | | ||
| + | $ 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 ''? | ||
| + | |||
| + | $ print "z exists?", | ||
| + | z exists? False | ||
| + | | ||
| + | $ z ?= 3 | ||
| + | | ||
| + | $ print "z exists?", | ||
| + | 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 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 ' | ||
| + | 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 z | ||
| + | |||
| + | ===== Flow control ===== | ||
| + | |||
| + | ==== if, elsif, else ==== | ||
| + | |||
| + | '' | ||
| + | |||
| + | if False | ||
| + | print ' | ||
| + | elsif False | ||
| + | print ' | ||
| + | elsif True > False | ||
| + | print ' | ||
| + | else | ||
| + | print ' | ||
| + | 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 '' | ||
| + | |||
| + | while True | ||
| + | print "I won't stay here." | ||
| + | break | ||
| + | print " | ||
| + | end | ||
| + | |||
| + | Optionally, a number argument can be given to '' | ||
| + | |||
| + | while True | ||
| + | while True | ||
| + | print " | ||
| + | break 2 | ||
| + | end | ||
| + | end | ||
| + | print " | ||
| + | |||
| + | ==== for ==== | ||
| + | |||
| + | '' | ||
| + | |||
| + | for i in [' | ||
| + | print i | ||
| + | end | ||
| + | print 'Value of i after the " | ||
| + | |||
| + | Output: | ||
| + | |||
| + | planes | ||
| + | trains | ||
| + | automobiles | ||
| + | Value of i after the " | ||
| + | |||
| + | When iterating a dictionary, the loop variable is an array containing the key and the value. | ||
| + | |||
| + | a = {' | ||
| + | for i in a | ||
| + | print i | ||
| + | end | ||
| + | |||
| + | Output: | ||
| + | |||
| + | [ a, 1 ] | ||
| + | [ b, 2 ] | ||
| + | |||
| + | ==== continue ==== | ||
| + | |||
| + | The '' | ||
| + | |||
| + | for i in [' | ||
| + | if i == ' | ||
| + | print i, 'are cool!' | ||
| + | end | ||
| + | |||
| + | Output: | ||
| + | |||
| + | planes are cool! | ||
| + | automobiles are cool! | ||
| + | |||
| + | |||
| + | |||