* Create control_flow.md * Rename control_flow.md to controlflow.md * Added control flow * Added classes * fix: corrected indentation * fix: corrected indentation
		
			
				
	
	
	
		
			1.7 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.7 KiB
		
	
	
	
	
	
	
	
title
| title | 
|---|
| Control Flow | 
Control Flow
Basic Usage
If Expression
if can be used in same way as in other programming languages
var min: Int
if (a < b) {
  min = a
} else {
  min = b
}
else can also be omitted if not needed
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
when (obj) {
  1          -> "One"
  1, 2       -> "One or Two"
  "Hello"    -> "Greeting"
  is Long    -> "Long"
  !is String -> "Not a string"
  else       -> "Unknown"
}
Range evaluation
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
val fruits = listOf("apple", "banana", "kiwi")
for (fruit in fruits) {
  println(fruit)
}
Iterating through range of numbers
for (i in 1..5) {
  println(i)
}
While Loops
while and do..while are used like in most programming languages
while (a > 0) {
  a--
}
do {
  val b = provideData()
} while (b != null)
Break and continue keywords
Used like in most other programming languages