Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

SJ-Nosrat
Copy link
Contributor

Update - julia/concepts.wip/if-expressions

  • Add blurb to config.json
  • Add intro to introduction.md
  • Add about to about.md

@SJ-Nosrat SJ-Nosrat requested a review from a team as a code owner February 6, 2023 09:18
Copy link
Contributor

@SaschaMann SaschaMann left a 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?

@SJ-Nosrat
Copy link
Contributor Author

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?

@SJ-Nosrat
Copy link
Contributor Author

SJ-Nosrat commented Feb 6, 2023

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 if statement in the about.md page. Please let me know if that's okay? Thanks for your valued feedback.

Comment on lines 14 to 32
## 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.
Copy link
Contributor

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)?

Copy link
Contributor Author

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.

Copy link
Contributor

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

@SaschaMann
Copy link
Contributor

I've added an example using only an if statement in the about.md page. Please let me know if that's okay? Thanks for your valued feedback.

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

```

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

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

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

Copy link
Contributor

@cmcaine cmcaine left a 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
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


~~~~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 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

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.

@@ -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.",

@@ -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:


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants