-
-
Notifications
You must be signed in to change notification settings - Fork 71
If expressions update #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e477ac
66cf9fb
bb03ead
e6e0e1e
61417b1
97ad190
c26627e
4116e66
72581e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"blurb": "TODO: add blurb for if-expressions concept", | ||
"authors": [], | ||
"blurb": "if-expressions control the flow of a code block by allowing to execute certain statements, given that the conditional expression is satisfied.", | ||
"authors": ["SJ-Nosrat"], | ||
"contributors": [] | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,52 +1,87 @@ | ||||||
# About | ||||||
|
||||||
`if`-expressions in Julia are similar to those seen in other languages: | ||||||
In Julia, like many other languages, allow for the following with `if`-expressions: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
- Chained if-expressions | ||||||
- Nested if-expressions | ||||||
|
||||||
~~~~exercism/note | ||||||
[`if`-expressions](https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation) as defined here will encompass the following syntaxes:`if`, `if-else` and `if-elseif-else` statements. | ||||||
~~~~ | ||||||
|
||||||
`if`-expressions allows for branching in our programs, given a condition is satisfied. | ||||||
|
||||||
## A first example | ||||||
|
||||||
The `function` defined below is not very realistic, but it shows the anatomy of an `if` statement. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```julia | ||||||
julia> function password_checker(password) | ||||||
if password == system_password | ||||||
return "Welcome back!" | ||||||
end | ||||||
end | ||||||
|
||||||
# Assume system_password is "abc123" | ||||||
julia> password_checker("abc123") | ||||||
Welcome back! | ||||||
|
||||||
julia> password_checker("xyz321") | ||||||
|
||||||
``` | ||||||
`println("Welcome back!")` is run, if the `password == system_password`, otherwise it is skipped, and nothing is printed to the screen. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Func returns a string now. Maybe leave out the bit about |
||||||
|
||||||
We can do the following for effective branching: | ||||||
|
||||||
## Chained expressions example | ||||||
|
||||||
```julia | ||||||
julia> function say_if_positive(n) | ||||||
if n > 0 | ||||||
println("n is positive!") | ||||||
else | ||||||
println("n is not positive!") | ||||||
end | ||||||
end | ||||||
say_if_positive (generic function with 1 method) | ||||||
|
||||||
julia> say_if_positive(10) | ||||||
n is positive! | ||||||
|
||||||
julia> say_if_positive(-10) | ||||||
n is not positive! | ||||||
# We can test for multiple cases | ||||||
# with chained if expression. | ||||||
if 0 ≤ n ≤ 0.5 | ||||||
println("n is in the interval [0, 0.5], inclusive.") | ||||||
elseif -0.5 ≤ n < 0 | ||||||
println("n is in the interval [-0.5, 0).") | ||||||
elseif n > 0.5 | ||||||
println("n is greater than 0.5.") | ||||||
else | ||||||
println("n is less than -0.5.") | ||||||
end | ||||||
``` | ||||||
|
||||||
<!-- TODO: Add that fancy concept highlight embed thing to boolean expression --> | ||||||
In the above chained expression, we can have mulitple `elseif` statements, with only one `else` statement at the end, but it's not necessary. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Nested expressions example | ||||||
|
||||||
If the boolean expression following the `if` evaluates to `true`, the first block of code is run and the second block is skipped. | ||||||
If the boolean expression following the `if` evaluates to `false`, the first block of code is skipped and the second block is run. | ||||||
The program continues running at the first line of code after the `end` keyword. | ||||||
```julia | ||||||
# We can refine our coniditonal evaluation | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to have the comments in the markdown rather than as code comments? There's a typo in conditional too |
||||||
# with nested if expressions. | ||||||
if n ≥ 0 | ||||||
if 0.5 ≤ n ≤ 1 | ||||||
println("n is in the interval [0.5, 1], inclusive.") | ||||||
else | ||||||
println("n is in the interval [0, 0.5).") | ||||||
end | ||||||
else | ||||||
println("n is negative!") | ||||||
end | ||||||
``` | ||||||
|
||||||
In cases where the second block of code would be just another `if`-expression, `elseif` allows us to avoid nesting `if`-expressions within the block: | ||||||
## Order matters | ||||||
|
||||||
```julia | ||||||
julia> function dessert(fruit) | ||||||
if fruit == "apple" | ||||||
return "Apple Crumble" | ||||||
elseif fruit == "lemon" | ||||||
return "Lemon Meringue Pie" | ||||||
else | ||||||
return "Fruit Salad" | ||||||
end | ||||||
end | ||||||
dessert (generic function with 1 method) | ||||||
|
||||||
julia> dessert("apple") | ||||||
"Apple Crumble" | ||||||
|
||||||
julia> dessert("lemon") | ||||||
"Lemon Meringue Pie" | ||||||
|
||||||
julia> dessert("peach") | ||||||
"Fruit Salad" | ||||||
# Suppose you guessed the number 2, | ||||||
# then the if block will be executed, | ||||||
# and the others skipped. | ||||||
if guess_number > 0 | ||||||
println("The number guessed is greater than 0.") | ||||||
elseif guess_number > 1 | ||||||
println("The number guessed is greater than 1.") | ||||||
else | ||||||
println("n is negative!") | ||||||
end | ||||||
``` | ||||||
In the above `if` expression, it is instructive to note that even though the conditional expression in the `elseif` clause would evaluate to `true`, it is skipped, | ||||||
since the conditional in the `if` clause is `true` first. | ||||||
|
||||||
If an `if`-expression only needs to perform code for one of the cases, there's no need to write out the `else` branch. | ||||||
As such sometimes you'll get unexpected results depending on how you structure your `if` expression. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,57 +1,86 @@ | ||||||
# Introduction | ||||||
|
||||||
`if`-expressions in Julia are similar to those seen in other languages: | ||||||
In Julia, `if`-expressions, are similar to those seen in other languages. | ||||||
|
||||||
~~~~exercism/note | ||||||
[`if`-expressions](https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation) as defined here will encompass the following syntaxes:`if`, `if-else` and `if-elseif-else` statements. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not statements |
||||||
~~~~ | ||||||
|
||||||
`if`-expressions provide the ability to execute certain statements in a _code block_ given that the condition is satisfied. In other words, it allows for branching in our programs. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## `if` statement example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```julia | ||||||
julia> function say_if_positive(n) | ||||||
# Defining a function, allows to pass different values | ||||||
# to test. The if statement is the code block inside | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# the function. | ||||||
julia> function say_positive(n) | ||||||
if n > 0 | ||||||
println("n is positive!") | ||||||
else | ||||||
println("n is not positive!") | ||||||
end | ||||||
end | ||||||
say_if_positive (generic function with 1 method) | ||||||
say_positive (generic function with 1 method) | ||||||
|
||||||
julia> say_if_positive(10) | ||||||
# 10 > 0 which enters the if block | ||||||
# and executes the println("n is positive!") | ||||||
# statement. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
julia> say_positive(10) | ||||||
n is positive! | ||||||
|
||||||
julia> say_if_positive(-10) | ||||||
n is not positive! | ||||||
# -10 < 0, if expression is skipped | ||||||
# and no print to screen. | ||||||
julia> say_positive(-10) | ||||||
``` | ||||||
`n > 0` following immediately the `if` keyword above is the conditional expression (a boolean expression), must always return `true` or `false` for the `if`-expression to be executed. | ||||||
|
||||||
<!-- TODO: Add that fancy concept highlight embed thing to boolean expression --> | ||||||
|
||||||
If the boolean expression following the `if` evaluates to `true`, the first block of code is run and the second block is skipped. | ||||||
If the boolean expression following the `if` evaluates to `false`, the first block of code is skipped and the second block is run. | ||||||
The program continues running at the first line of code after the `end` keyword. | ||||||
|
||||||
~~~~exercism/note | ||||||
In Julia, the `end` keyword signifies the end of all block expressions. | ||||||
This syntax is not specific to `if`-expressions or function definitions. | ||||||
~~~~ | ||||||
|
||||||
In cases where the second block of code would be just another `if`-expression, `elseif` allows us to avoid nesting `if`-expressions within the block: | ||||||
## `if`-`else` statement example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```julia | ||||||
julia> function dessert(fruit) | ||||||
if fruit == "apple" | ||||||
return "Apple Crumble" | ||||||
elseif fruit == "lemon" | ||||||
return "Lemon Meringue Pie" | ||||||
else | ||||||
return "Fruit Salad" | ||||||
julia> function say_if_positive(n) | ||||||
if n > 0 | ||||||
println("n is positive!") | ||||||
else | ||||||
println("n is negative!") | ||||||
end | ||||||
end | ||||||
|
||||||
say_if_positive (generic function with 1 method) | ||||||
|
||||||
# Again, -10 < 0, the if block is skipped, | ||||||
# else block is executed, as the only other | ||||||
# alternative. | ||||||
julia> say_if_positive(-10) | ||||||
n is negative! | ||||||
``` | ||||||
## `if`-`elseif`-`else` statement example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```julia | ||||||
julia> function say_if_positive_or_zero(n) | ||||||
if n > 0 | ||||||
println("n is positive!") | ||||||
elseif n < 0 | ||||||
println("n is negative!") | ||||||
else | ||||||
println("n is zero!") | ||||||
end | ||||||
end | ||||||
dessert (generic function with 1 method) | ||||||
|
||||||
julia> dessert("apple") | ||||||
"Apple Crumble" | ||||||
say_if_positive_or_zero (generic function with 1 method) | ||||||
|
||||||
julia> dessert("lemon") | ||||||
"Lemon Meringue Pie" | ||||||
# We explicitly test for n to be negative number | ||||||
# in the elseif block. | ||||||
julia> say_if_positive_or_zero(-3) | ||||||
n is negative! | ||||||
|
||||||
julia> dessert("peach") | ||||||
"Fruit Salad" | ||||||
# 0 is neither positive nor negative, | ||||||
# else block is executed. | ||||||
julia> say_if_positive_or_zero(0) | ||||||
n is zero! | ||||||
``` | ||||||
|
||||||
If an `if`-expression only needs to perform code for one of the cases, there's no need to write out the `else` branch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.