Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Conditional Statements: if and when | Functions and Control Flow
Practice
Projects
Quizzes & Challenges
Quizzen
Challenges
/
Introduction to Kotlin

bookConditional Statements: if and when

Veeg om het menu te tonen

Conditional statements allow you to control the flow of your Kotlin programs by making decisions based on certain conditions. The two main conditional statements in Kotlin are if and when. Both help you execute different blocks of code depending on the values of variables or the results of expressions.

The if statement is used to check a condition and execute code only if that condition is true. It can also include else and else if branches to handle alternative cases.

The when statement is Kotlin's more flexible version of the traditional switch statement found in other languages. It allows you to match a value against a series of conditions and execute code based on the first matching branch. This can make your code cleaner and easier to read when you have multiple possible conditions to check.

Below, you will find a Kotlin example that demonstrates both if and when statements in action.

Main.kt

Main.kt

copy
12345678910111213141516171819202122232425
package com.example fun main() { val temperature = 30 val weatherDescription: String // Using if statement if (temperature > 25) { println("It's hot outside.") } else if (temperature in 16..25) { println("The weather is moderate.") } else { println("It's cold outside.") } // Using when statement weatherDescription = when (temperature) { in 26..40 -> "Sunny and warm" in 16..25 -> "Pleasant" in 0..15 -> "Chilly" else -> "Extreme weather" } println("Weather description: $weatherDescription") }

In the code above, you first see an if statement that checks the value of the temperature variable. If the temperature is greater than 25, the program prints "It's hot outside." If the temperature is between 16 and 25 (inclusive), it prints "The weather is moderate." Otherwise, it prints "It's cold outside." This demonstrates how if, else if, and else branches work together to handle multiple conditions.

Next, the when statement assigns a value to the weatherDescription variable based on the range in which the temperature falls. If the temperature is between 26 and 40, weatherDescription becomes "Sunny and warm". If it is between 16 and 25, it becomes "Pleasant", and if it is between 0 and 15, it becomes "Chilly". The else branch covers any other values, assigning "Extreme weather".

The main difference between if and when is in their usage: use if for simple true/false decisions or a small number of conditions, and use when for checking a value against several possibilities, which makes your code more readable and maintainable when dealing with multiple cases.

question mark

Which statement about Kotlin conditional statements is true?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 3. Hoofdstuk 2
some-alt