-
-
Notifications
You must be signed in to change notification settings - Fork 203
Add Conditionals concept #710
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"contributors": [], | ||
"blurb": "The conditionals `if`, `else if` and `else` are used to control the flow of execution and make decisions in a program." | ||
} |
This file contains hidden or 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,82 @@ | ||
# About conditionals | ||
|
||
## Comparison operators | ||
|
||
[Comparison operators][operators] are similar to many other languages, with a few extensions. | ||
|
||
For equality, the operators are `==` (equal) and `!=` (not equal). | ||
|
||
```Kotlin | ||
val txt = "abc" | ||
txt == "abc" // => true | ||
txt != "abc" // => false | ||
``` | ||
|
||
Additionally, the `===` and `!==` operators test for ["referential equality"][referential-equality]: | ||
`a === b` if and only if `a` and `b` point to the same object. | ||
This should make more sense later in the syllabus. | ||
|
||
The greater/less than operators are also conventional. | ||
|
||
```Kotlin | ||
1 < 3 // => true | ||
3 > 3 // => false | ||
3 <= 3 // => true | ||
4 >= 3 // => true | ||
``` | ||
|
||
## Branching with `if` | ||
|
||
This is the full form of an [`if` expression][if-else]: | ||
|
||
```Kotlin | ||
if (conditional1) { | ||
// something... | ||
} else if (conditional2) { | ||
// something... | ||
} else { | ||
// something... | ||
} | ||
``` | ||
|
||
- Parentheses `()` around each conditional are required. | ||
- A conditional must evaluate to a Boolean `true` or `false`. | ||
Kotlin has no concept of "truthy" and "falsy" as found in some languages. | ||
- Braces `{}` are optional, if there is only a single expression. | ||
- Both `else if` and `else` are optional, and there can be multiple `else if` blocks. | ||
|
||
|
||
## Alternatives? | ||
|
||
By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java. | ||
A concise form of `if ... else` is preferred: | ||
|
||
```Kotlin | ||
val result = if (isOk) goodValue else badValue | ||
|
||
return if (isOK) goodValue else badValue | ||
``` | ||
|
||
Unlike Ruby, the concise `if ... else` form *always* needs an `else` and thus cannot be used as a guard statement: | ||
|
||
```kotlin | ||
return 42 if (isOk) | ||
// Syntax error: Unexpected tokens (use ';' to separate expressions on the same line). | ||
// Syntax error: Expecting an expression. | ||
|
||
return if (isOk) true | ||
// 'if' must have both main and 'else' branches when used as an expression. | ||
``` | ||
|
||
Note that in Kotlin, `if` is an [_expression_][expression] returning a value. | ||
It is not a [_statement_][statement] as in Java. | ||
|
||
We will see in a later Concept that Kotlin has a powerful [`when`][when] construct, intended to replace long chains of `else if` clauses with pattern matching. | ||
|
||
|
||
[operators]: https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols | ||
[referential-equality]: https://kotlinlang.org/docs/equality.html#floating-point-numbers-equality | ||
[if-else]: https://kotlinlang.org/docs/control-flow.html#if-expression | ||
[when]: https://kotlinlang.org/docs/control-flow.html#when-expressions-and-statements | ||
[expression]: https://en.wikipedia.org/wiki/Expression_(computer_science) | ||
[statement]: https://en.wikipedia.org/wiki/Statement_(computer_science) |
This file contains hidden or 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,52 @@ | ||
# Introduction | ||
|
||
## Comparison operators | ||
|
||
Comparison operators are similar to many other languages. | ||
|
||
For equality, the operators are `==` (equal) and `!=` (not equal). | ||
|
||
```Kotlin | ||
val txt = "abc" | ||
txt == "abc" // => true | ||
txt != "abc" // => false | ||
``` | ||
|
||
The greater/less than operators are also conventional. | ||
|
||
```Kotlin | ||
1 < 3 // => true | ||
3 > 3 // => false | ||
3 <= 3 // => true | ||
4 >= 3 // => true | ||
``` | ||
|
||
## Branching with `if` | ||
|
||
This is the full form of an `if` expression: | ||
|
||
```Kotlin | ||
if (conditional1) { | ||
// something... | ||
} else if (conditional2) { | ||
// something... | ||
} else { | ||
// something... | ||
} | ||
``` | ||
|
||
- Parentheses `()` around each conditional are required. | ||
- A conditional must evaluate to a Boolean `true` or `false`. | ||
Kotlin has no concept of "truthy" and "falsy" as found in some languages. | ||
- Braces `{}` are optional, if there is only a single expression. | ||
- Both `else if` and `else` are optional, and there can be multiple `else if` blocks. | ||
|
||
|
||
## Alternatives? | ||
|
||
By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in Java. | ||
A concise form of `if ... else` is preferred: | ||
|
||
```Kotlin | ||
return if (isOK) goodValue else badValue | ||
``` |
This file contains hidden or 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,14 @@ | ||
[ | ||
{ | ||
"url": "https://kotlinlang.org/docs/keyword-reference.html#operators-and-special-symbols", | ||
"description": "Operators reference." | ||
}, | ||
{ | ||
"url": "https://kotlinlang.org/docs/control-flow.html#if-expression", | ||
"description": "Control flow introduction." | ||
}, | ||
{ | ||
"url": "https://kotlinlang.org/docs/coding-conventions.html#control-flow-statements", | ||
"description": "Coding conventions." | ||
} | ||
] |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.