# Kotlin Basics: Control Flow: if, when, for, while
###### tags: `Kotlin` `Control Flows`
[TOC]
# Introduction
Independantly of the programming language you are using, you will definitely have 4 basics control flow:**if, when, for, while**
# Control Flow
## If Statement
In Kotlin, using if alone you can just return a value, that is what we call if as an expression.
```kotlin=
// As expression
val amount = if (a > b) a else b
```
>Like any other language, you can ommit the {}, however it is not a good style. In my opinion, it is much more readable and easy to understand having them.
Furtheremore, if you want to add another function inside that same if statement then there will no be any problems.[color=orange]
```kotlin=
// Not recommended usage
var amount = a
if (a < b) amount = b
// Recommended usage with else
var amount: Int
if (a > b) {
amount = a
} else {
amount = b
}
```
## When Statement
When statement is what we call **Switch** in Java/C. You get a variable that will go inside the when() brackets, and below are the different cases that you may encounter.
What makes it different from switch is that default is shown as else.
>**Note:**
>Same as if statement, you can ommit the {}, all depends in your coding style.[color=lightPink]
```kotlin=
when (x) {
0, 1 -> {
print("x = 1 || x = 0")
}
2 -> {
print("x == 2")
}
else -> { // Note the block
print("x is neither 0 nor 1 nor 2")
}
}
```
**What makes kotlin more different?**
1. Cases can be shown as arbitrary expressions (not only constants), this means you can put functions inside. Ex: parseInt()
```kotlin=
val num : String = "3"
when(x){
parseInt(num) -> print("num is x String mode")
else -> print("num is another number")
}
```
2. Cases can be check within or out of a range
```kotlin=
when(x){
in 0..3 -> {
print("0 <= x <= 3")
}
!in 4..8 -> {
print("x is not between 4~8")
}
else -> { // Note the block
print("x is neither of cases above")
}
}
```
3. Cases can be checked by seeing if it **is** or **!is**(is not) fullfiling a type of property.
```kotlin=
when(x){
is Intenger -> print("x is an Intenger")
else -> print("x is another Type")
}
when(x){
!is String -> print("x is not of type String")
else -> print("x is a String")
}
```
4. **Kotlin's When** can replace **if else Statatement**
```kotlin=
when{
x.isOdd() -> print("x is an odd number")
x.isEven() -> print("x is an even number")
}
```
> **isOdd() & isEven()** are methods already included inside Kotlin [color=lightBlue]
5. Can Merge http request with When since Kotlin 1.3
```kotlin=
fun Request.getBody() = when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}
```
## For Loop
### List Loop
**For those that need only the item itself:**
```kotlin=
val itemList : ArrayList<Item>
for (item: Item in itemList) {
// ...
}
```
**For those that need both item && position:**
```kotlin=
val test : List<String> = arrayListOf()
for ((index, value) in test.withIndex()) {
println("the element at $index is $value")
}
```
### Number Loop
```kotlin=
for (i in 1..3) {
println(i)
}
//Prints 1 to 3
for (i in 6 downTo 0 step 2) {
println(i)
}
//Prints 6, 4, 2, 0
//Note: downTo makes it goes in reverse
```
## Do While Loop
while && do while in kotlin is the same as java.
```kotlin=
while (x > 0) {
x--
}
do {
val y = retrieveData()
} while (y != null) // y is visible here!
```