edited control flow section

This commit is contained in:
Leah Hanson 2013-07-01 16:59:53 -04:00
parent 2a2dae61ce
commit 2627624bb4

View File

@ -233,57 +233,63 @@ contains(filled_set,10) #=> false
## 3. Control Flow ## 3. Control Flow
#################################################### ####################################################
# Let's just make a variable # Let's make a variable
some_var = 5 some_var = 5
# Here is an if statement. INDENTATION IS SIGNIFICANT IN PYTHON! # Here is an if statement. Indentation is NOT meaningful in Julia.
# prints "some var is smaller than 10" # prints "some var is smaller than 10"
if some_var > 10: if some_var > 10
print "some_var is totally bigger than 10." println("some_var is totally bigger than 10.")
elif some_var < 10: # This elif clause is optional. elseif some_var < 10 # This elseif clause is optional.
print "some_var is smaller than 10." println("some_var is smaller than 10.")
else: # This is optional too. else # This is optional too.
print "some_var is indeed 10." println("some_var is indeed 10.")
end
"""
For loops iterate over lists
prints:
dog is a mammal
cat is a mammal
mouse is a mammal
"""
for animal in ["dog", "cat", "mouse"]:
# You can use % to interpolate formatted strings
print "%s is a mammal" % animal
""" # For loops iterate over iterable things, such as ranges, lists, sets, dicts, strings.
While loops go until a condition is no longer met. # prints:
prints: # dog is a mammal
0 # cat is a mammal
1 # mouse is a mammal
2
3 for animal=["dog", "cat", "mouse"]
""" # You can use $ to interpolate into strings
println("$animal is a mammal")
end
# You can use in instead of =, if you want.
for animal in ["dog", "cat", "mouse"]
println("$animal is a mammal")
end
for a in ["dog"=>"mammal","cat"=>"mammal","mouse"=>"mammal"]
println("$(a[1]) is $(a[2])")
end
# While loops go until a condition is no longer met.
# prints:
# 0
# 1
# 2
# 3
x = 0 x = 0
while x < 4: while x < 4
print x println(x)
x += 1 # Shorthand for x = x + 1 x += 1 # Shorthand for x = x + 1
end
# Handle exceptions with a try/except block # Handle exceptions with a try/except block
# Works on Python 2.6 and up: error("help") # ERROR: help in error at error.jl:21
try:
# Use raise to raise an error
raise IndexError("This is an index error")
except IndexError as e:
pass # Pass is just a no-op. Usually you would do recovery here.
# Works for Python 2.7 and down: try
try: error("my error!")
raise IndexError("This is an index error") except
except IndexError, e: # No "as", comma instead println("caught it!")
pass end
#################################################### ####################################################