diff --git a/concepts.wip/if-expressions/.meta/config.json b/concepts.wip/if-expressions/.meta/config.json index d57937d0..ad7432e5 100644 --- a/concepts.wip/if-expressions/.meta/config.json +++ b/concepts.wip/if-expressions/.meta/config.json @@ -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": [] } diff --git a/concepts.wip/if-expressions/about.md b/concepts.wip/if-expressions/about.md index 2184571a..713914c1 100644 --- a/concepts.wip/if-expressions/about.md +++ b/concepts.wip/if-expressions/about.md @@ -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: + +- 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. + +```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. + +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 ``` - +In the above chained expression, we can have mulitple `elseif` statements, with only one `else` statement at the end, but it's not necessary. + +## 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 +# 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. \ No newline at end of file diff --git a/concepts.wip/if-expressions/introduction.md b/concepts.wip/if-expressions/introduction.md index 33bd5768..ac7828e8 100644 --- a/concepts.wip/if-expressions/introduction.md +++ b/concepts.wip/if-expressions/introduction.md @@ -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. +~~~~ + +`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 ```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 +# 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. +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. - - -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 ```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 + +```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.