-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5. Control Flow.jl
137 lines (106 loc) · 4.15 KB
/
5. Control Flow.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# In this chapter we stop to writing console output in comments.
# To see result of action select it and click on run selection button.
#******************************************************************************#
# 5.1 Begin and (;) #
#******************************************************************************#
# Sometimes you can need expression which evaluates several subexpressions.
# For this you can use begin blocks and (;) chains.
expression = begin
x =1
y = 2
z = 3
x + y + z
end
expression = (x =1; y = 2; z = 3; x + y + z)
#******************************************************************************#
# 5.2 If statement #
#******************************************************************************#
var = 5
if var > 5
println("var is bigger than 5")
elseif var < 5
println("var is smaller than 5")
else
println("var is indeed 5")
end
# This statement prints "var is indeed 5".
# Also you can use shorter version of if,
# but it's only for statements if -> else
# condition?action_for_true:action_for_false
var==5?println("var = 5"):println("var != 5")
# And you can nest this statements:
x = 1
y = 3
println(x < y ? "x is less than y" :
x > y ? "x is greater than y" : "x is equal to y")
#******************************************************************************#
# 5.3 For loop #
#******************************************************************************#
# This loop iterate over iteretables.
for fruit=["apple", "cherry", "peach"]
println("$fruit")
end
# You can use "in" instead of "=":
for number in [1:5;]
println("$number")
end
# For loop can iterate over Range, Array, Set, Dict and AbstractString
for dict in Dict("apple" => "green", "cherry" => "red", "peach" => "orange")
println("$(dict[1]) is $(dict[2])")
end
# You can write it another:
for (x,y) in Dict("apple" => "green", "cherry" => "red", "peach" => "orange")
println("$x is $y")
end
#******************************************************************************#
# 5.4 While loop #
#******************************************************************************#
# This loop loops while condition is true.
# e.g. while(true) loops to infinity.
var = 1
while var < 10
println(var)
var += 1
end
#******************************************************************************#
# 5.4 Try-catch statement #
#******************************************************************************#
# Try-catch statement helps us when we want to handle errors.
try
something_wrong
catch e
println(e)
end
#******************************************************************************#
# 5.4 Finally clauses #
#******************************************************************************#
# When you start to working with files you propably want to easy close file
# after operations. You can do it with finally keyword
#
# try
# operations
# finally
# operations after try block
# end
#******************************************************************************#
# Conclusion #
#******************************************************************************#
# You can nest if statement, for lopps and while loops:
var = 5
while var >= 0
for (x,y) in Dict("apple" => "green", "cherry" => "red", "peach" => "orange")
if var > 0
if var > 1
println("$x is $y")
end
end
end
println()
var -= 1
end
# This example only shows how you can do it. Don't coding like this!
# It's better to write short and optimalized code
# You can read more about control flow on:
# http://docs.julialang.org/en/release-0.5/manual/control-flow/
# There you can find more details about things from this chapter and
# more useful things (e.g. tasks, events).