summaryrefslogtreecommitdiffhomepage
path: root/julia.html.markdown
diff options
context:
space:
mode:
authorLeah Hanson <astrieanna@gmail.com>2013-07-01 16:59:53 -0400
committerLeah Hanson <astrieanna@gmail.com>2013-07-01 16:59:53 -0400
commit2627624bb495a0d5450e493b685a938c70eaa32d (patch)
treef262792eb5a32b2064306fa2558e8e64d27d6ac4 /julia.html.markdown
parent2a2dae61ce885d57fc257b28c5c041076041cf9e (diff)
edited control flow section
Diffstat (limited to 'julia.html.markdown')
-rw-r--r--julia.html.markdown92
1 files changed, 49 insertions, 43 deletions
diff --git a/julia.html.markdown b/julia.html.markdown
index b19250af..12f52e6e 100644
--- a/julia.html.markdown
+++ b/julia.html.markdown
@@ -233,57 +233,63 @@ contains(filled_set,10) #=> false
## 3. Control Flow
####################################################
-# Let's just make a variable
+# Let's make a variable
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"
-if some_var > 10:
- print "some_var is totally bigger than 10."
-elif some_var < 10: # This elif clause is optional.
- print "some_var is smaller than 10."
-else: # This is optional too.
- print "some_var is indeed 10."
-
-
-"""
-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
-
-"""
-While loops go until a condition is no longer met.
-prints:
- 0
- 1
- 2
- 3
-"""
+if some_var > 10
+ println("some_var is totally bigger than 10.")
+elseif some_var < 10 # This elseif clause is optional.
+ println("some_var is smaller than 10.")
+else # This is optional too.
+ println("some_var is indeed 10.")
+end
+
+
+
+# For loops iterate over iterable things, such as ranges, lists, sets, dicts, strings.
+# prints:
+# dog is a mammal
+# cat is a mammal
+# mouse is a mammal
+
+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
-while x < 4:
- print x
+while x < 4
+ println(x)
x += 1 # Shorthand for x = x + 1
+end
# Handle exceptions with a try/except block
-# Works on Python 2.6 and up:
-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:
- raise IndexError("This is an index error")
-except IndexError, e: # No "as", comma instead
- pass
+error("help") # ERROR: help in error at error.jl:21
+
+try
+ error("my error!")
+except
+ println("caught it!")
+end
####################################################