Skip to content

Commit

Permalink
new concept booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleach committed Sep 8, 2024
1 parent 8dbe20e commit 6cf0ac9
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 0 deletions.
9 changes: 9 additions & 0 deletions concepts/booleans/.meta/config.json
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)."
}
107 changes: 107 additions & 0 deletions concepts/booleans/about.md
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/
84 changes: 84 additions & 0 deletions concepts/booleans/introduction.md
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/
6 changes: 6 additions & 0 deletions concepts/booleans/links.json
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"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,11 @@
"slug": "functions",
"name": "Functions"
},
{
"uuid": "813fb2fa-0068-4167-93d6-e8b8e7b7b55c",
"slug": "booleans",
"name": "Booleans"
},
{
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",
"slug": "integer-introduction",
Expand Down

0 comments on commit 6cf0ac9

Please sign in to comment.