From 1e99510ebccd9d55093c5f1873195d90639f55a3 Mon Sep 17 00:00:00 2001 From: Samuel Koprivnjak Date: Sun, 26 May 2019 14:27:17 +0200 Subject: [PATCH] Control Flow for Kotlin lang (#26793) * Create control_flow.md * Rename control_flow.md to controlflow.md * Added control flow * Added classes * fix: corrected indentation * fix: corrected indentation --- guide/english/kotlin/classes/index.md | 44 +++++++++++ guide/english/kotlin/control-flow/index.md | 89 ++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 guide/english/kotlin/classes/index.md create mode 100644 guide/english/kotlin/control-flow/index.md diff --git a/guide/english/kotlin/classes/index.md b/guide/english/kotlin/classes/index.md new file mode 100644 index 00000000000..47719c41ada --- /dev/null +++ b/guide/english/kotlin/classes/index.md @@ -0,0 +1,44 @@ +--- +title: Classes +--- + +# Classes + +### Basic Usage + +#### Declaration + +`class` keyword is used to define the class +It is consisted of: class name, class header (type parameters, constructor) and class body (curly braces) +```kotlin +class Person { ... } +``` + +#### Constructor + +There are multiple ways to define class constructor. +Primary constructor is part of the class header (name field with data type): +```kotlin +class Person(name: String) { ... } +``` + +Secondary constructor: using `constructor` keyword inside class body +```kotlin +class Person { + constructor(parent: Person) { + parent.children.add(this) + } +} +``` + +#### Usage + +Class instance can be created as regular function +There is no need for `new` keyword +```kotlin +val john = Person("John Wayne") +``` + +#### Resources +* [Basic Syntax Reference](https://kotlinlang.org/docs/reference/basic-syntax.html) +* [Kotlin Classes Reference](https://kotlinlang.org/docs/reference/classes.html) diff --git a/guide/english/kotlin/control-flow/index.md b/guide/english/kotlin/control-flow/index.md new file mode 100644 index 00000000000..b699ba12187 --- /dev/null +++ b/guide/english/kotlin/control-flow/index.md @@ -0,0 +1,89 @@ +--- +title: Control Flow +--- + +# Control Flow + +### Basic Usage + +#### If Expression + +`if` can be used in same way as in other programming languages +```kotlin +var min: Int +if (a < b) { + min = a +} else { + min = b +} +``` + +`else` can also be omitted if not needed +```kotlin +if (a < b) { + print("a is smaller") +} +``` + +#### When Expression + +`when` keyword is replacing usual `switch-case` expression +The else branch is evaluated if none of the other branch conditions are satisfied +It has powerfull matching branches which support complex evaluations of input argument +```kotlin +when (obj) { + 1 -> "One" + 1, 2 -> "One or Two" + "Hello" -> "Greeting" + is Long -> "Long" + !is String -> "Not a string" + else -> "Unknown" +} +``` + +Range evaluation +```kotlin +when (x) { + in 1..100 -> print("x is in the range") + in validNumbers -> print("x is valid") + !in 10..200 -> print("x is outside the range") + else -> print("none of the above") +} +``` + +#### For Loops + +`for` loop can iterate through anything that provides and interator, using `in` operator +```kotlin +val fruits = listOf("apple", "banana", "kiwi") +for (fruit in fruits) { + println(fruit) +} +``` + +Iterating through range of numbers +```kotlin +for (i in 1..5) { + println(i) +} +``` + +#### While Loops + +`while` and `do..while` are used like in most programming languages +```kotlin +while (a > 0) { + a-- +} + +do { + val b = provideData() +} while (b != null) +``` + +#### Break and continue keywords +Used like in most other programming languages + +#### Resources +* [Basic Syntax Reference](https://kotlinlang.org/docs/reference/basic-syntax.html) +* [Kotlin Control Flow Reference](https://kotlinlang.org/docs/reference/control-flow.html)