-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
If expressions update #550
base: main
Are you sure you want to change the base?
Conversation
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.
I like the examples! I'm thinking perhaps it makes sense to have a very simple example without else
or else-if
above the more complicated ones. What do you think?
Sure, that's a good idea. Where do you want to put the examples in? Intro or about page? or Both? |
I've added an example using only an |
concepts.wip/if-expressions/about.md
Outdated
## A prompt program example | ||
|
||
Suppose you've defined a `password_checker(password)` function that returns `true`, if the password entered by the user is correct. | ||
|
||
```julia | ||
# Login prompt | ||
println("Enter Password: ") | ||
|
||
# Get user input | ||
password = readline() | ||
|
||
# Our main logic | ||
if password_checker(password) | ||
println("Welcome!") | ||
end | ||
``` | ||
Although the above program is not very realistic, but it shows the anatomy of an `if` statement. | ||
|
||
In this case the `println("Welcome!")` is run, if the `password_checker(password)` returns `true`, otherwise it is skipped. |
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.
This is mostly a personal view, not an Exercism view, even though some other maintainers agree:
Imho any examples with reading input or printing anything should be avoided completely. So many people new to programming struggle with understanding the difference between printing and returning a value, often because their introductory classes or texts use printing everywhere. This is even more confusing in REPL settings where the difference is visually indistinguishable. To avoid this, I'd only print in an exercise that's explicitly about IO, not general examples or other exercises.
The example itself works but perhaps it could be turned into a function password_check(password)
?
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.
Thank you for explaining this. Understood, I've converted the example into a function
, as suggested. Please let me know if the example is clear.
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.
That's clearer, though I'd change it to return a string
Works great! |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
`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.
``` | ||
|
||
<!-- 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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 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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
[`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](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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
`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`-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` statement example |
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.
## `if` statement example | |
## `if` example |
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.
Thanks for the PR! Here are some suggestions. Don't feel like you have to apply them.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
## `if`-`elseif`-`else` statement example | |
## `if`-`elseif`-`else` example |
|
||
~~~~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 comment
The reason will be displayed to describe this comment to others. Learn more.
## `if`-`else` statement example | |
## `if`-`else` 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 |
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.
# to test. The if statement is the code block inside | |
# to test. The if expression is the code block inside |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
# statement. | |
# expression. |
@@ -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.", |
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.
"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.", |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
In Julia, like many other languages, allow for the following with `if`-expressions: | |
Julia, like many other languages, allows for the following `if`-expressions: |
|
||
## 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
Update -
julia/concepts.wip/if-expressions
config.json
introduction.md
about.md