Skip to content

Commit 580c414

Browse files
committed
Rework if/2 docs
1 parent 9c56121 commit 580c414

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,48 +3898,40 @@ defmodule Kernel do
38983898
38993899
## One-liner examples
39003900
3901-
if(foo, do: bar)
3901+
iex> if 7 > 5, do: "yes"
3902+
"yes"
39023903
39033904
In the example above, `bar` will be returned if `foo` evaluates to
39043905
a truthy value (neither `false` nor `nil`). Otherwise, `nil` will be
39053906
returned.
39063907
3908+
iex> if 3 > 5, do: "yes"
3909+
nil
3910+
39073911
An `else` option can be given to specify the opposite:
39083912
3909-
if(foo, do: bar, else: baz)
3913+
iex> if 3 > 5, do: "yes", else: "no"
3914+
"no"
39103915
39113916
## Blocks examples
39123917
39133918
It's also possible to pass a block to the `if/2` macro. The first
39143919
example above would be translated to:
39153920
3916-
if foo do
3917-
bar
3918-
end
3921+
iex> if 7 > 5 do
3922+
...> "yes"
3923+
...> end
3924+
"yes"
39193925
39203926
Note that `do`-`end` become delimiters. The second example would
39213927
translate to:
39223928
3923-
if foo do
3924-
bar
3925-
else
3926-
baz
3927-
end
3928-
3929-
## Examples
3930-
3931-
iex> if 5 > 7 do
3932-
...> "This will never be returned"
3929+
iex> if 3 > 5 do
3930+
...> "yes"
39333931
...> else
3934-
...> "This will be returned"
3932+
...> "no"
39353933
...> end
3936-
"This will be returned"
3937-
3938-
iex> if 2 + 2 == 4, do: :correct
3939-
:correct
3940-
3941-
iex> if 2 + 2 == 5, do: :correct
3942-
nil
3934+
"no"
39433935
39443936
If you find yourself nesting conditionals inside conditionals,
39453937
consider using `cond/1`.

0 commit comments

Comments
 (0)