From a83ae14cbe94c4ff11b3423b18d5b76a3643790d Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Fri, 29 Aug 2025 16:11:46 -0700 Subject: [PATCH 1/2] Add Conditionals concept --- concepts/conditionals/.meta/config.json | 7 +++ concepts/conditionals/about.md | 69 +++++++++++++++++++++++++ concepts/conditionals/introduction.md | 52 +++++++++++++++++++ concepts/conditionals/links.json | 14 +++++ config.json | 5 ++ 5 files changed, 147 insertions(+) create mode 100644 concepts/conditionals/.meta/config.json create mode 100644 concepts/conditionals/about.md create mode 100644 concepts/conditionals/introduction.md create mode 100644 concepts/conditionals/links.json diff --git a/concepts/conditionals/.meta/config.json b/concepts/conditionals/.meta/config.json new file mode 100644 index 00000000..eff83475 --- /dev/null +++ b/concepts/conditionals/.meta/config.json @@ -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." +} diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md new file mode 100644 index 00000000..8da84723 --- /dev/null +++ b/concepts/conditionals/about.md @@ -0,0 +1,69 @@ +# 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 +return if (isOK) goodValue else badValue +``` + +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) diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md new file mode 100644 index 00000000..ec754fb6 --- /dev/null +++ b/concepts/conditionals/introduction.md @@ -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 +``` diff --git a/concepts/conditionals/links.json b/concepts/conditionals/links.json new file mode 100644 index 00000000..b02ba3d2 --- /dev/null +++ b/concepts/conditionals/links.json @@ -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." + } +] diff --git a/config.json b/config.json index 25127b19..ba7a0c1d 100644 --- a/config.json +++ b/config.json @@ -1382,6 +1382,11 @@ "uuid": "53e0b729-810e-4042-9477-8c8d05a7b302", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "168827c0-4867-449a-ad22-611c87314c48", + "slug": "conditionals", + "name": "Conditionals" } ], "key_features": [ From 597cd138b1a6003ace97241fe981baa6b8dbbd4c Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Fri, 29 Aug 2025 16:49:13 -0700 Subject: [PATCH 2/2] Update about.md with Kotlin if...else details I like the suggestions, which are now added. --- concepts/conditionals/about.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 8da84723..88d30d65 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -52,9 +52,22 @@ By deliberate choice, Kotlin does _not_ have the ternary operator `? :` found in 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.