-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Colin Leach
committed
Oct 2, 2024
1 parent
a467cbb
commit 8ef16ce
Showing
4 changed files
with
96 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"blurb": "Conditionals execute certain statements depending on the return value of a conditional expression.", | ||
"authors": ["SJ-Nosrat"], | ||
"contributors": [] | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"contributors": [], | ||
"blurb": "The conditionals 'if', 'elseif' ('else + if'), and 'else' are used to control the flow of execution and make decisions in a program." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,90 @@ | ||
# About | ||
|
||
Conditional evaluation is when certain checks have to be made to see if a _conditional_ (expession) is satisfied before we can execute a set of statements in our program. | ||
## Comparison operators | ||
|
||
This is where Julia provides the following ways to control the flow of our program. | ||
[Comparison operators in Julia][numeric-comparisons] are similar to many other languages, though with some extra options for math-lovers. | ||
|
||
There are three primary conditional statements that are used in Julia: | ||
For equality, the operators are `==` (equal) and `!=` or `≠` (not equal). | ||
|
||
- [`if`-expressions](https://exercism.org/tracks/julia/concepts/if-expressions) | ||
- [The ternary operator](https://exercism.org/tracks/julia/concepts/ternary-operator) | ||
- [Short-circuit evaluation](https://exercism.org/tracks/julia/concepts/short-circuit-evaluation) | ||
```julia | ||
txt = "abc" | ||
txt == "abc" # true | ||
txt != "abc" # false | ||
txt ≠ "abc" # false (synonym for !=) | ||
``` | ||
|
||
Take note of the following, in Julia the _conditional expression_ must return `true` or `false`. Otherwise it is an error. | ||
In addition, we have the various greater/less than operators. | ||
|
||
```julia-repl | ||
julia> if 1 | ||
println("true") | ||
end | ||
ERROR: TypeError: non-boolean (Int64) used in boolean context | ||
``` | ||
```julia | ||
1 < 3 # true | ||
3 > 3 # false | ||
3 <= 3 # true | ||
3 ≤ 3 # true (synonym for <=) | ||
4 >= 3 # true | ||
4 ≥ 3 # true (synonym for >=) | ||
``` | ||
|
||
As often with Julia, an appropriate editor makes use of the mathematical symbol easy. | ||
Type `\ne`, `\le` or `\ge` then `TAB` to get `≠`, `≤` or `≥`. | ||
|
||
The previous example uses only numbers, but we will see in other parts of the syllabus that various additional types have a sense of ordering and can be tested for greater/less than. | ||
|
||
Comparison operators can be chained, which allows a clear and concise syntax: | ||
|
||
```julia | ||
n = 3 | ||
1 ≤ n ≤ 5 # true (n "between" two limits) | ||
``` | ||
|
||
The previous example is a synonym for `1 ≤ n && n ≤ 5`. | ||
|
||
## Branching with `if` | ||
|
||
This is the full form of an [`if` statement][conditional-eval]: | ||
|
||
```julia | ||
if conditional1 | ||
statements... | ||
elseif conditional2 | ||
statements... | ||
else | ||
statements... | ||
end | ||
``` | ||
|
||
There is no need for parentheses `()` or braces `{}`, and indentation is "only" to improve readability _(but readability is very important!)_. | ||
|
||
Both `elseif` and `else` are optional, and there can be multiple `elseif` blocks. | ||
However, the `end` is required. | ||
|
||
It is possible to nest `if` statements, though you might want to help readability with the thoughtful use of parentheses, indents and comments. | ||
|
||
The shortest form of an `if` statement would be something like this: | ||
|
||
```julia | ||
if n < 0 | ||
n = 0 | ||
end | ||
``` | ||
|
||
As a reminder: only expressions that evaluate to `true` or `false` can be used as conditionals. | ||
Julia deliberately avoids any concept of "truthiness", so zero values, empty strings and empty arrays are _not_ equivalent to `false`. | ||
|
||
## Ternary operator | ||
|
||
A simple and common situation is picking one of two values based on a conditional. | ||
|
||
Julia, like many languages, has a ternary operator to make this more concise. | ||
|
||
The syntax is `conditional ? value_if_true : value_if_false`. | ||
|
||
So the previous example could be rewritten: | ||
|
||
```julia | ||
n = n < 0 ? 0 : n | ||
``` | ||
|
||
Parentheses are not required by the compiler, but may improve readability. | ||
|
||
[numeric-comparisons]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons | ||
[conditional-eval]: https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1 @@ | ||
# Introduction | ||
|
||
Conditional evaluation is one of the handful of ways that Julia offers to _control the flow_ of your program. | ||
|
||
There are three primary ways to achieve this in Julia: | ||
|
||
- `if`-expressions | ||
- The ternary operator | ||
- Short-circuit evaluation | ||
|
||
Suppose you need a program to execute a certain set of statements, given that the conditional expression is satisfied, then we structure the code according to the syntax that Julia provides, as listed above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
[ | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation", | ||
"description": "Julia Manual" | ||
} | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons", | ||
"description": "Numeric comparisons" | ||
}, | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation", | ||
"description": "Conditional evaluation" | ||
} | ||
] |