-
-
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
1 parent
8dbe20e
commit 6cf0ac9
Showing
5 changed files
with
211 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"contributors": [ | ||
"SaschaMann" | ||
], | ||
"blurb": "Julia has boolean values true and false, operators ! (not), && (and), || (or)." | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# About | ||
|
||
## Booleans in Julia | ||
|
||
True or false values are represented by the `Bool` type. | ||
It contains only two values: `true` and `false`. | ||
|
||
```julia-repl | ||
julia> true | ||
true | ||
julia> false | ||
false | ||
julia> typeof(true) | ||
Bool | ||
``` | ||
|
||
## Boolean logic | ||
|
||
Imagine we have the following Boolean expressions in Julia: `5 > x` and `x != 0`. | ||
If `x` was 3 they would both be `true`. | ||
We can express statements like "is x less than 5 and not equal to y?" using [Boolean operators][boolean-operators]: `!` (not), `&&` (and), `||` (or). | ||
|
||
In Julia (and many other programming languages), `&&` has a [higher precedence][operator-precedence] than `||` (in the same way that `*` is applied before `+`). | ||
This means that `true || false && true` evaluates to `true` because it is parsed as `(true || false) && true`. | ||
It is common to include explicit brackets anyway so that the reader doesn't need to think about this. | ||
|
||
### Logical _not_ | ||
|
||
`!` represents the logical "not" operation in Julia. | ||
Not is also called negation. | ||
|
||
```julia-repl | ||
julia> !false | ||
true | ||
julia> false != true | ||
true | ||
julia> 3 != "apple" | ||
true | ||
julia> !(false == true) | ||
true | ||
julia> !(1 < 7) | ||
false | ||
``` | ||
|
||
### Logical _and_ | ||
|
||
`&&` (two ampersands) represents logical "and" in Julia. | ||
|
||
```julia-repl | ||
julia> 5 > 3 | ||
true | ||
julia> 1 != 0 | ||
true | ||
julia> (5 > 3) && (1 != 0) | ||
true | ||
``` | ||
|
||
Parentheses are optional and can make the code easier to read. | ||
|
||
### Logical _or_ | ||
|
||
`||` (two pipe characters) represents logical "or" in Julia. | ||
|
||
```julia-repl | ||
julia> 5 * 5 == 25 | ||
true | ||
julia> 2 < 1 | ||
false | ||
julia> (5 * 5 == 25) || (2 < 1) | ||
true | ||
``` | ||
|
||
### Short-circuit evaluation | ||
|
||
When `&&` or `||` are used to link Boolean expressions, Julia evaluates from left to right, and stops when it finds an unambiguous answer. | ||
|
||
Consider this expression: | ||
|
||
```julia-repl | ||
julia> (5 > 12) && (1 != 0) | ||
true | ||
``` | ||
|
||
Because `5 > 12` is `false`, clearly the combined expression must be `false`. | ||
The `1 != 0` expression is not relevant and is never evaluated. | ||
|
||
This is quite often used as a shortcut to trap runtime problems and edge cases, in the form: | ||
|
||
```julia | ||
all_ok || do_something() | ||
``` | ||
|
||
For example, the `do_something` might be an early `return` from the function if `all_ok` is `false`, or assigning a default value to a variable before continuing. | ||
|
||
|
||
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity | ||
[boolean-operators]: https://docs.julialang.org/en/ |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Introduction | ||
|
||
## Booleans in Julia | ||
|
||
True or false values are represented by the `Bool` type. | ||
It contains only two values: `true` and `false`. | ||
|
||
```julia-repl | ||
julia> true | ||
true | ||
julia> false | ||
false | ||
julia> typeof(true) | ||
Bool | ||
``` | ||
|
||
## Boolean logic | ||
|
||
Imagine we have the following Boolean expressions in Julia: `5 > x` and `x != 0`. | ||
If `x` was 3 they would both be `true`. | ||
We can express statements like "is x less than 5 and not equal to y?" using [Boolean operators][boolean-operators]: `!` (not), `&&` (and), `||` (or). | ||
|
||
In Julia (and many other programming languages), `&&` has a [higher precedence][operator-precedence] than `||` (in the same way that `*` is applied before `+`). | ||
This means that `true || false && true` evaluates to `true` because it is parsed as `(true || false) && true`. | ||
It is common to include explicit brackets anyway so that the reader doesn't need to think about this. | ||
|
||
### Logical _not_ | ||
|
||
`!` represents the logical "not" operation in Julia. | ||
Not is also called negation. | ||
|
||
```julia-repl | ||
julia> !false | ||
true | ||
julia> false != true | ||
true | ||
julia> 3 != "apple" | ||
true | ||
julia> !(false == true) | ||
true | ||
julia> !(1 < 7) | ||
false | ||
``` | ||
|
||
### Logical _and_ | ||
|
||
`&&` (two ampersands) represents logical "and" in Julia. | ||
|
||
```julia-repl | ||
julia> 5 > 3 | ||
true | ||
julia> 1 != 0 | ||
true | ||
julia> (5 > 3) && (1 != 0) | ||
true | ||
``` | ||
|
||
Parentheses are optional and can make the code easier to read. | ||
|
||
### Logical _or_ | ||
|
||
`||` (two pipe characters) represents logical "or" in Julia. | ||
|
||
```julia-repl | ||
julia> 5 * 5 == 25 | ||
true | ||
julia> 2 < 1 | ||
false | ||
julia> (5 * 5 == 25) || (2 < 1) | ||
true | ||
``` | ||
|
||
[operator-precedence]: https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity | ||
[boolean-operators]: https://docs.julialang.org/en/ |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[ | ||
{ | ||
"url": "https://docs.julialang.org/en/v1/manual/mathematical-operations/#Boolean-Operators", | ||
"description": "Boolean Operators, section in the manual" | ||
} | ||
] |
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