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

[Booleans] new concept #781

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
colinleach marked this conversation as resolved.
Show resolved Hide resolved

### Logical _not_

`!` represents the logical "not" operation in Julia.
colinleach marked this conversation as resolved.
Show resolved Hide resolved
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.
colinleach marked this conversation as resolved.
Show resolved Hide resolved

Consider this expression:

```julia-repl
julia> (5 > 12) && (1 != 0)
true
```

Because `5 > 12` is `false`, clearly the combined expression must be `false`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe we can say the combined expression is clearly false since we haven't introduced why the expression would be false. We also risk reinforcing whatever interpretation the student got from line 85 because they might think Julia stops when it hits a false value regardless of which operator is used so that's what "unambiguous" means.

The `1 != 0` expression is not relevant and is never evaluated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 != 0 isn't evaluated when the left side evaluates to false. It is evaluated if the left side evaluates to true so we can't say it's never evaluated without qualifying that never.


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`.
colinleach marked this conversation as resolved.
Show resolved Hide resolved
It is common to include explicit brackets anyway so that the reader doesn't need to think about this.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is common to include explicit brackets anyway so that the reader doesn't need to think about this.
It is common to include explicit parentheses 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be included under the logical OR section. However, we've already mentioned this in the Boolean logic section so it can be dropped.


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