Skip to content

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

Closed
wants to merge 9 commits into from
4 changes: 2 additions & 2 deletions concepts.wip/if-expressions/.meta/config.json
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.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"blurb": "if-expressions control the flow of a code block by allowing to execute certain statements, given that the conditional expression is satisfied.",
"blurb": "if-expressions let the program choose whether to execute an expression or not based on some condition.",

"authors": ["SJ-Nosrat"],
"contributors": []
}
115 changes: 75 additions & 40 deletions concepts.wip/if-expressions/about.md
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In Julia, like many other languages, allow for the following with `if`-expressions:
Julia, like many other languages, allows for the following `if`-expressions:


- 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `function` defined below is not very realistic, but it shows the anatomy of an `if` statement.
The `function` defined below is not very realistic, but it shows the anatomy of an `if` expression.


```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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`println("Welcome back!")` is run, if the `password == system_password`, otherwise it is skipped, and nothing is printed to the screen.
`"Welcome back!"` is returned if `password == system_password`, otherwise that block is skipped and `nothing` is returned.

Func returns a string now. Maybe leave out the bit about nothing being returned.


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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In the above chained expression, we can have mulitple `elseif` statements, with only one `else` statement at the end, but it's not necessary.
In a chained if/elseif/else expression, we can have zero or more `elseif` expressions, and at most one `else` expression at the end.


## 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
Copy link
Contributor

@cmcaine cmcaine Feb 8, 2023

Choose a reason for hiding this comment

The 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.
87 changes: 58 additions & 29 deletions concepts.wip/if-expressions/introduction.md
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[`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](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`.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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.
`if`-expressions provide the ability to execute certain expression in a _code block_ given that the condition is satisfied. In other words, it allows for branching in our programs.


## `if` statement example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## `if` statement example
## `if` example


```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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# to test. The if statement is the code block inside
# to test. The if expression is the code block inside

# 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# statement.
# expression.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## `if`-`else` statement example
## `if`-`else` example


```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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## `if`-`elseif`-`else` statement example
## `if`-`elseif`-`else` example


```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.