You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/pages/getting-started/case-cond-and-if.md
+50-46Lines changed: 50 additions & 46 deletions
Original file line number
Diff line number
Diff line change
@@ -67,51 +67,10 @@ iex> case :ok do
67
67
68
68
The documentation for the `Kernel` module lists all available guards in its sidebar. You can also consult the complete [Patterns and Guards](../references/patterns-and-guards.md#guards) reference for in-depth documentation.
69
69
70
-
## cond
71
-
72
-
`case` is useful when you need to match against different values. However, in many circumstances, we want to check different conditions and find the first one that does not evaluate to `nil` or `false`. In such cases, one may use `cond`:
73
-
74
-
```elixir
75
-
iex>conddo
76
-
...>2+2==5->
77
-
...>"This will not be true"
78
-
...>2*2==3->
79
-
...>"Nor this"
80
-
...>1+1==2->
81
-
...>"But this will"
82
-
...>end
83
-
"But this will"
84
-
```
85
-
86
-
This is equivalent to `else if` clauses in many imperative languages - although used less frequently in Elixir.
87
-
88
-
If all of the conditions return `nil` or `false`, an error (`CondClauseError`) is raised. For this reason, it may be necessary to add a final condition, equal to `true`, which will always match:
89
-
90
-
```elixir
91
-
iex>conddo
92
-
...>2+2==5->
93
-
...>"This is never true"
94
-
...>2*2==3->
95
-
...>"Nor this"
96
-
...>true->
97
-
...>"This is always true (equivalent to else)"
98
-
...>end
99
-
"This is always true (equivalent to else)"
100
-
```
101
-
102
-
Finally, note `cond` considers any value besides `nil` and `false` to be true:
103
-
104
-
```elixir
105
-
iex>conddo
106
-
...>hd([1, 2, 3]) ->
107
-
...>"1 is considered as true"
108
-
...>end
109
-
"1 is considered as true"
110
-
```
111
-
112
70
## if/unless
113
71
114
-
Besides `case` and `cond`, Elixir also provides `if/2` and `unless/2`, which are useful when you need to check for only one condition:
72
+
73
+
`case` builds on pattern matching and guards to destructure and match on certain conditions. However, patterns and guards are limited only to certain expressions which are optimized by the compiler. In many situations, you need to write conditions that go beyond what can be expressed with `case`. For those, `if/2` (and `unless/2`) are useful alternatives:
115
74
116
75
```elixir
117
76
iex>iftruedo
@@ -126,7 +85,7 @@ nil
126
85
127
86
If the condition given to `if/2` returns `false` or `nil`, the body given between `do`-`end` is not executed and instead it returns `nil`. The opposite happens with `unless/2`.
128
87
129
-
They also support `else` blocks:
88
+
They also support `else` blocks (although using `else` with `unless` is generally discouraged):
130
89
131
90
```elixir
132
91
iex>ifnildo
@@ -167,5 +126,50 @@ iex> x = if true do
167
126
>
168
127
> An interesting note regarding `if/2` and `unless/2` is that they are implemented as macros in the language: they aren't special language constructs as they would be in many languages. You can check the documentation and their source for more information.
169
128
170
-
We have concluded the introduction to the most fundamental control-flow constructs in Elixir. Now
171
-
let's learn where code and data meet with anonymous functions.
129
+
If you find yourself nesting several `if/2` blocks, you may want to consider using `cond/1` instead. Let's check it out.
130
+
131
+
## cond
132
+
133
+
If you need to check across several conditions and find the first one that does not evaluate to `nil` or `false`, `cond/1` is a useful construct:
134
+
135
+
```elixir
136
+
iex>conddo
137
+
...>2+2==5->
138
+
...>"This will not be true"
139
+
...>2*2==3->
140
+
...>"Nor this"
141
+
...>1+1==2->
142
+
...>"But this will"
143
+
...>end
144
+
"But this will"
145
+
```
146
+
147
+
This is equivalent to `else if` clauses in many imperative languages - although used less frequently in Elixir.
148
+
149
+
If all of the conditions return `nil` or `false`, an error (`CondClauseError`) is raised. For this reason, it may be necessary to add a final condition, equal to `true`, which will always match:
150
+
151
+
```elixir
152
+
iex>conddo
153
+
...>2+2==5->
154
+
...>"This is never true"
155
+
...>2*2==3->
156
+
...>"Nor this"
157
+
...>true->
158
+
...>"This is always true (equivalent to else)"
159
+
...>end
160
+
"This is always true (equivalent to else)"
161
+
```
162
+
163
+
Similar to `if/2` and `unless/2`, `cond` considers any value besides `nil` and `false` to be true:
164
+
165
+
```elixir
166
+
iex>conddo
167
+
...>hd([1, 2, 3]) ->
168
+
...>"1 is considered as true"
169
+
...>end
170
+
"1 is considered as true"
171
+
```
172
+
173
+
## Summing up
174
+
175
+
We have concluded the introduction to the most fundamental control-flow constructs in Elixir. Generally speaking, Elixir developers prefer pattern matching and guards, using `case` and function definitions (which we will explore in future chapters). When your logic cannot be expressed within patterns and guards, you may consider `if/2`, falling back to `cond/1` when there are several conditions to check.
0 commit comments