Skip to content

Commit a775732

Browse files
clauddzyclauddzy
and
clauddzy
authored
Update unless / else example in conditionals concept (#1742)
* Update else statement examples * Fix typos * Update cascading if example --------- Co-authored-by: clauddzy <[email protected]>
1 parent a59f7e8 commit a775732

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

concepts/conditionals/about.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Here is a list of the operators and an example of when they give a `true` value:
1717

1818
| Method | Description | Example |
1919
| ------ | --------------------- | ------- |
20-
| < | less than | 5 < 4 |
20+
| < | less than | 4 < 5 |
2121
| <= | less than or equal | 4 <= 4 |
2222
| > | greater than | 3 > 1 |
2323
| >= | greater than or equal | 2 >= 2 |
@@ -66,7 +66,7 @@ end
6666

6767
## Unless statement
6868

69-
The `unless`unless statement works very similarly to the `if` statement but it will run the code inside the `unless` statement if the condition is falsey.
69+
The `unless` statement works very similarly to the `if` statement but it will run the code inside the `unless` statement if the condition is falsey.
7070

7171
```ruby
7272
value = 1
@@ -83,8 +83,8 @@ end
8383

8484
## Else statement
8585

86-
The `else` statement can be used in conjunction with the `if` and `unless` statements.
87-
The `else` statement will be executed if the `if` branch or the `unless` branch is not executed.
86+
The `else` statement can be used in conjunction with the `if` statement.
87+
The `else` statement will be executed if the `if` branch is not executed.
8888

8989
```ruby
9090
value = 1
@@ -95,29 +95,31 @@ else
9595
end
9696
# => "1 is equal to 1"
9797

98-
unless value < 2
98+
if value > 2
99+
"1 is greater than 2"
100+
else
99101
"1 is not greater than 2"
100102
end
101-
# => "1 is greater than 2"
103+
# => "1 is not greater than 2"
102104
```
103105

104106
## "Cascading-if" statements
105107

106108
The `elsif` statement can be used in conjunction with the if statement.
107109
The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy.
108110
Elsif statements can be chained together and the first truthy condition will be executed.
109-
There can also be an else statement at the end of the if statement which will run if non of the earlier statement has been true.
111+
There can also be an else statement at the end of the if statement which will run if none of the earlier statements have been true.
110112

111113
```ruby
112114
value = 1
113-
if value != 1
114-
"1 is not equal to 1"
115+
if value == 0
116+
"1 is equal to 0"
115117
elsif value > 2
116118
"1 is greater than 2"
117119
else
118-
"1 is not equal to 1 and 1 is not greater than 2"
120+
"1 is not equal to 0 and 1 is not greater than 2"
119121
end
120-
# => "1 is not equal to 1 and 1 is not greater than 2"
122+
# => "1 is not equal to 0 and 1 is not greater than 2"
121123
```
122124

123125
## if and unless as suffix

concepts/conditionals/introduction.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Here is a list of the operators and an example of when they give a `true` value:
1717

1818
| Method | Description | Example |
1919
| ------ | --------------------- | ------- |
20-
| < | less than | 5 < 4 |
20+
| < | less than | 4 < 5 |
2121
| <= | less than or equal | 4 <= 4 |
2222
| > | greater than | 3 > 1 |
2323
| >= | greater than or equal | 2 >= 2 |
@@ -53,7 +53,7 @@ end
5353

5454
## Unless statement
5555

56-
The `unless`unless statement works very similarly to the `if` statement but it will run the code inside the `unless` statement if the condition is falsey.
56+
The `unless` statement works very similarly to the `if` statement but it will run the code inside the `unless` statement if the condition is falsey.
5757

5858
```ruby
5959
value = 1
@@ -70,8 +70,8 @@ end
7070

7171
## Else statement
7272

73-
The `else` statement can be used in conjunction with the `if` and `unless` statements.
74-
The `else` statement will be executed if the `if` branch or the `unless` branch is not executed.
73+
The `else` statement can be used in conjunction with the `if` statement.
74+
The `else` statement will be executed if the `if` branch is not executed.
7575

7676
```ruby
7777
value = 1
@@ -82,29 +82,31 @@ else
8282
end
8383
# => "1 is equal to 1"
8484

85-
unless value < 2
85+
if value > 2
86+
"1 is greater than 2"
87+
else
8688
"1 is not greater than 2"
8789
end
88-
# => "1 is greater than 2"
90+
# => "1 is not greater than 2"
8991
```
9092

9193
## "Cascading-if" statements
9294

9395
The `elsif` statement can be used in conjunction with the if statement.
9496
The `elsif` statement will be executed if the if branch is not executed and the condition of the elsif statement is truthy.
9597
Elsif statements can be chained together and the first truthy condition will be executed.
96-
There can also be an else statement at the end of the if statement which will run if non of the earlier statement has been true.
98+
There can also be an else statement at the end of the if statement which will run if none of the earlier statements have been true.
9799

98100
```ruby
99101
value = 1
100-
if value != 1
101-
"1 is not equal to 1"
102+
if value == 0
103+
"1 is equal to 0"
102104
elsif value > 2
103105
"1 is greater than 2"
104106
else
105-
"1 is not equal to 1 and 1 is not greater than 2"
107+
"1 is not equal to 0 and 1 is not greater than 2"
106108
end
107-
# => "1 is not equal to 1 and 1 is not greater than 2"
109+
# => "1 is not equal to 0 and 1 is not greater than 2"
108110
```
109111

110112
[comparison-operators]: https://www.w3resource.com/ruby/ruby-comparison-operators.php

0 commit comments

Comments
 (0)