<commit_before>---
type: doc
layout: reference
category: "Syntax"
title: "Control Flow: if, when, for, while"
---

# Control Flow: if, when, for, while

## If Expression

In Kotlin, *if*{: .keyword } is an expression, i.e. it returns a value.
Therefore there is no ternary operator (condition ? then : else), because ordinary *if*{: .keyword } works fine in this role.

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
// Traditional usage 
var max = a 
if (a < b) max = b

// With else 
var max: Int
if (a > b) {
    max = a
} else {
    max = b
}
 
// As expression 
val max = if (a > b) a else b
```

</div>

*if*{: .keyword } branches can be blocks, and the last expression is the value of a block:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
val max = if (a > b) {
    print("Choose a")
    a
} else {
    print("Choose b")
    b
}
```

</div>

If you're using *if*{: .keyword } as an expression rather than a statement (for example, returning its value or
assigning it to a variable), the expression is required to have an `else` branch.

See the [grammar for *if*{: .keyword }](grammar.html#ifExpression).

## When Expression

*when*{: .keyword } replaces the switch operator of C-like languages. In the simplest form it looks like this

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}
```

</div>

*when*{: .keyword } matches its argument against all branches sequentially until some branch condition is satisfied.
*when*{: .keyword } can be used either as an expression or as a statement. If it is used as an expression, the value
of the satisfied branch becomes the value of the overall expression. If it is used as a statement, the values of<commit_msg>fix: switch is a statement in C control-flow.md (#1845)<commit_after>---
type: doc
layout: reference
category: "Syntax"
title: "Control Flow: if, when, for, while"
---

# Control Flow: if, when, for, while

## If Expression

In Kotlin, *if*{: .keyword } is an expression, i.e. it returns a value.
Therefore there is no ternary operator (condition ? then : else), because ordinary *if*{: .keyword } works fine in this role.

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
// Traditional usage 
var max = a 
if (a < b) max = b

// With else 
var max: Int
if (a > b) {
    max = a
} else {
    max = b
}
 
// As expression 
val max = if (a > b) a else b
```

</div>

*if*{: .keyword } branches can be blocks, and the last expression is the value of a block:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
val max = if (a > b) {
    print("Choose a")
    a
} else {
    print("Choose b")
    b
}
```

</div>

If you're using *if*{: .keyword } as an expression rather than a statement (for example, returning its value or
assigning it to a variable), the expression is required to have an `else` branch.

See the [grammar for *if*{: .keyword }](grammar.html#ifExpression).

## When Expression

The *when*{: .keyword } expression replaces the switch statement in C-like languages. In the simplest form it looks like this

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}
```

</div>

*when*{: .keyword } matches its argument against all branches sequentially until some branch condition is satisfied.
*when*{: .keyword } can be used either as an expression or as a statement. If it is used as an expression, the value
of the satisfied branch becomes the value of the overall expression. If it is used as a statement, the values of
{'fraction_non_alphanumeric': 0.13136033229491173, 'fraction_numerical': 0.005711318795430945, 'mean_word_length': 3.383390216154721, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7374255', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1193, 'n_words': 545}