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 indentationpull/36133/head
parent
f5b94b51f1
commit
1e99510ebc
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue