1. Before you begin
Conditionals are one of the most important foundations of programming. Conditionals are commands in programming languages that handle decisions. With conditionals, code is dynamic, which means that it can behave differently given a different condition.
This codelab teaches you how to use the if/else
and when
statements and expressions to write conditionals in Kotlin.
Prerequisites
- Knowledge of Kotlin programming basics, including variables, and the
println()
andmain()
functions
What you'll learn
- How to write boolean expressions.
- How to write
if/else
statements. - How to write
when
statements. - How to write
if/else
expressions. - How to write
when
expressions. - How to use commas to define common behavior for multiple branches in
when
conditionals. - How to use the
in
range to define common behavior for a range of branches inwhen
conditionals. - How to use the
is
keyword to writewhen
conditional statements.
What you'll need
- A web browser with access to Kotlin Playground
2. Use if/else statements to express conditions
In life, it's common to do things differently based on the situation that you face. For example, if the weather is cold, you wear a jacket, whereas if the weather is warm, you don't wear a jacket.
Decision-making is also a fundamental concept in programming. You write instructions about how a program should behave in a given situation so that it can act or react accordingly when the situation occurs. In Kotlin, when you want your program to perform different actions based on a condition, you can use an if/else
statement. In the next section, you write an if
statement.
Write if
conditions with boolean expressions
Imagine that you build a program that tells drivers what they should do when they're at a traffic light. Focus on the first condition: a red traffic light. What do you do at a red traffic light? Stop!
In Kotlin, you can express this condition with an if
statement. Take a look at the anatomy of an if
statement:
To use if
statements, you need to use the if
keyword followed by the condition that you want to evaluate. You need to express the condition with a boolean expression. Expressions combine values, variables, and operators that return a value. Boolean expressions return a boolean value.
Previously, you learned about assignment operators, such as:
val number = 1
The =
assignment operator assigns the number
variable a 1
value.
In contrast, boolean expressions are constructed with comparison operators, which compare values or variables on both sides of the equation. Take a look at a comparison operator.
1 == 1
The ==
comparison operator compares the values to each other. Which boolean value do you think this expression returns?
Find the boolean value of this expression:
- Use Kotlin Playground to run your code.
- In the function body, add a
println()
function and then pass it the1 == 1
expression as an argument:
fun main() {
println(1 == 1)
}
- Run the program and then view the output:
true
The first 1
value is equal to the second 1
value, so the boolean expression returns a true
value, which is a boolean value.
Try it
Besides the ==
comparison operator, there are additional comparison operators that you can use to create boolean expressions:
- Less than:
<
- Greater than:
>
- Less than or equal to:
<=
- Greater than or equal to:
>=
- Not equal to:
!=
Practice the use of comparison operators with simple expressions:
- In the argument, replace the
==
comparison operator with the<
comparison operator:
fun main() {
println(1 < 1)
}
- Run the program and then view the output:
The output returns a false
value because the first 1
value isn't less than the second 1
value.
false
- Repeat the first two steps with the other comparison operators and numbers.
Write a simple if
statement
Now that you saw a few examples of how to write boolean expressions, you can write your first if
statement. The syntax for an if
statement is as follows:
An if
statement starts with the if
keyword followed by a condition, which is a boolean expression inside parentheses and a set of curly braces. The body is a series of statements or expressions that you put inside a pair of curly braces after the condition. These statements or expressions only execute when the condition is met. In other words, the statements within the curly braces only execute when a boolean expression in the if
branch returns a true
value.
Write an if
statement for the red traffic-light condition:
- Inside the
main()
function, create atrafficLightColor
variable and assign it a"Red"
value:
fun main() {
val trafficLightColor = "Red"
}
- Add an
if
statement for the red traffic-light condition and then pass it atrafficLightColor == "Red"
expression:
fun main() {
val trafficLightColor = "Red"
if (trafficLightColor == "Red") {
}
}
- In the body of the
if
statement, add aprintln()
function and then pass it a"Stop"
argument:
fun main() {
val trafficLightColor = "Red"
if (trafficLightColor == "Red") {
println("Stop")
}
}
- Run the program and then view the output:
Stop
The trafficLightColor == "Red"
expression returns a true
value, so the println("Stop")
statement is executed, which prints the Stop
message.
Add an else
branch
Now you can extend the program so that it tells drivers to go when the traffic light isn't red.
You need to add an else
branch to create an if/else
statement. A branch is an incomplete part of code that you can join to form statements or expressions. An else
branch needs to follow an if
branch.
After the closing curly brace of the if
statement, you add the else
keyword followed by a pair of curly braces. Inside the curly braces of the else
statement, you can add a second body that only executes when the condition in the if
branch is false.
Add an else
branch to your program:
- After the closing curly brace of the
if
statement, add theelse
keyword followed by another pair of curly braces:
fun main() {
val trafficLightColor = "Red"
if (trafficLightColor == "Red") {
println("Stop")
} else {
}
}
- Inside the
else
keyword's curly braces, add aprintln()
function and then pass it a"Go"
argument:
fun main() {
val trafficLightColor = "Red"
if (trafficLightColor == "Red") {
println("Stop")
} else {
println("Go")
}
}
- Run this program and then view the output:
Stop
The program still behaves the same way as it did before you added the else
branch, but it doesn't print a Go
message.
- Reassign the
trafficLightColor
variable to a"Green"
value because you want drivers to go on green:
fun main() {
val trafficLightColor = "Green"
if (trafficLightColor == "Red") {
println("Stop")
} else {
println("Go")
}
}
- Run this program and then view the output:
Go
As you can see, now the program prints a Go
message instead of a Stop
message.
You reassigned the trafficLightColor
variable to a "Green"
value, so the trafficLightColor == "Red"
expression evaluated in the if
branch returns a false
value because the "Green"
value isn't equal to the "Red"
value.
As a result, the program skips all statements in the if
branch and instead executes all statements inside the else
branch. This means that the println("Go")
function is executed, but the println("Stop")
function isn't executed.
Add an else if
branch
Typically, a traffic light also has a yellow color that tells drivers to proceed slowly. You can expand the program's decision-making process to reflect this.
You learned to write conditionals that cater to a single decision point with if/else
statements that contain a single if
and a single else
branch. How can you handle more complex branching with multiple decision points? When you face multiple decision points, you need to create conditionals with multiple layers of conditions, which you can do when you add else if
branches to if/else
statements.
After the closing curly brace of the if
branch, you need to add the else if
keyword. Inside the parentheses of the else if
keyword, you need to add a boolean expression as the condition for the else if
branch followed by a body inside a pair of curly braces. The body is only executed if condition 1 fails, but condition 2 is satisfied.
The else if
branch is always located after the if
branch, but before the else
branch. You can use multiple else if
branches in a statement:
The if
statement can also contain the if
branch and else if
branches without any else
branch:
Add an else if
branch to your program:
- After the closing curly brace of the
if
statement, add anelse if (trafficLightColor == "Yellow")
expression followed by curly braces:
fun main() {
val trafficLightColor = "Green"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
} else {
println("Go")
}
}
- Inside the curly braces of the
else if
branch, add aprintln()
statement and then pass it a"Slow"
string argument:
fun main() {
val trafficLightColor = "Green"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else {
println("Go")
}
}
- Reassign the
trafficLightColor
variable to a"Yellow"
string value:
fun main() {
val trafficLightColor = "Yellow"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else {
println("Go")
}
}
- Run this program and then view the output:
Slow
Now the program prints a Slow
message, instead of a Stop
or Go
message.
Here's why it only prints a Slow
message and not the other lines:
- The
trafficLightColor
variable is assigned a"Yellow"
value. - The
"Yellow"
value isn't equal to the"Red"
value, so the boolean expression of theif
branch (denoted as 1 in the image) returns afalse
value. The program skips all statements inside theif
branch and doesn't print aStop
message. - Since the
if
branch yields afalse
value, the program proceeds to evaluate the boolean expression inside theelse if
branch. - The
"Yellow"
value is equal to the"Yellow"
value, so the boolean expression of theelse if
branch (denoted as 2 in the image) returns atrue
value. The program executes all statements inside theelse if
branch and prints aSlow
message. - Since the boolean expression of the
else if
branch returns atrue
value, the program skips the rest of the branches. Thus, all statements in theelse
branch aren't executed and the program doesn't print aGo
message.
Try it
Have you noticed that the current program contains a bug?
In Unit 1, you learned about a type of bug called a compile error in which Kotlin can't compile the code due to a syntactical error in your code and the program can't run. Here, you face another type of bug called a logic error in which the program can run, but doesn't produce an output as intended.
Supposedly, you only want drivers to drive only when the traffic-light color is green. What if the traffic light is broken and turned off? Would you want the driver to drive or receive a warning that something is wrong?
Unfortunately, in the current program, if the traffic-light color is anything else other than red or yellow, the driver is still advised to go.
Fix this problem:
- Reassign the
trafficLightColor
variable to a"Black"
value to illustrate a traffic light that is turned off:
fun main() {
val trafficLightColor = "Black"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else {
println("Go")
}
}
- Run this program and then view the output:
Go
Notice that the program prints a Go
message even though the trafficLightColor
variable isn't assigned a "Green"
value. Can you fix this program so that it reflects the correct behavior?
You need to modify the program so that it prints:
- A
Go
message only when thetrafficLightColor
variable is assigned a"Green"
value. - An
Invalid traffic-light color
message when thetrafficLightColor
variable isn't assigned a"Red"
,"Yellow"
, or"Green"
value.
Fix the else
branch
The else
branch is always located at the end of an if/else
statement because it's a catchall branch. It automatically executes when all other conditions in the preceding branches aren't satisfied. As such, the else
branch isn't suitable when you want an action to execute only when it satisfies a specific condition. In the case of the traffic light, you can use the else if
branch to specify the condition for green lights.
Use the else if
branch to evaluate the green traffic-light condition:
- After the current
else if
branch, add anotherelse if (trafficLightColor == "Green")
branch:
fun main() {
val trafficLightColor = "Black"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else if (trafficLightColor == "Green") {
println("Go")
}
}
- Run this program and then view the output.
The output is empty because you don't have an else
branch that executes when the previous conditions aren't satisfied.
- After the last
else if
branch, add anelse
branch with aprintln("Invalid traffic-light color")
statement inside:
fun main() {
val trafficLightColor = "Black"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else if (trafficLightColor == "Green") {
println("Go")
} else {
println("Invalid traffic-light color")
}
}
- Run this program and then view the output:
Invalid traffic-light color
- Assign the
trafficLightColor
variable another value besides"Red"
,"Yellow"
, or"Green"
and then rerun the program.
What's the output of the program?
It's good programming practice to have an explicit else if
branch as input validation for the green color and an else
branch to catch other invalid inputs. This ensures that drivers are directed to go, only when the traffic light is green. For other cases, there's an explicit message relayed that the traffic light isn't behaving as expected.
3. Use a when statement for multiple branches
Your trafficLightColor
program looks more complex with multiple conditions, also known as branching. You may wonder whether you can simplify a program with an even larger number of branches.
In Kotlin, when you deal with multiple branches, you can use the when
statement instead of the if/else
statement because it improves readability, which refers to how easy it is for human readers, typically developers, to read the code. It's very important to consider readability when you write your code because it's likely that other developers need to review and modify your code throughout its lifetime. Good readability ensures that developers can correctly understand your code and don't inadvertently introduce bugs into it.
when
statements are preferred when there are more than two branches to consider.
A when
statement accepts a single value through the parameter. The value is then evaluated against each of the conditions sequentially. The corresponding body of the first condition that's met is then executed. Each condition and body are separated by an arrow (->
). Similar to if/else
statements, each pair of condition and body is called a branch in when
statements. Also similar to the if/else
statement, you can add an else
branch as your final condition in a when
statement that works as a catchall branch.
Rewrite an if/else
statement with a when
statement
In the traffic-light program, there are already multiple branches:
- Red traffic-light color
- Yellow traffic-light color
- Green traffic-light color
- Other traffic-light color
Convert the program to use a when
statement:
- In the
main()
function, remove theif/else
statement:
fun main() {
val trafficLightColor = "Black"
}
- Add a
when
statement and then pass it thetrafficLightColor
variable as an argument:
fun main() {
val trafficLightColor = "Black"
when (trafficLightColor) {
}
}
- In the body of the
when
statement, add the"Red"
condition followed by an arrow and aprintln("Stop")
body:
fun main() {
val trafficLightColor = "Black"
when (trafficLightColor) {
"Red" -> println("Stop")
}
}
- On the next line, add the
"Yellow"
condition followed by an arrow and aprintln("Slow")
body:
fun main() {
val trafficLightColor = "Black"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
}
}
- On the next line, add the
"Green"
condition followed by an arrow and then aprintln("Go")
body:
fun main() {
val trafficLightColor = "Black"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
"Green" -> println("Go")
}
}
- On the next line, add the
else
keyword followed by an arrow and then aprintln("Invalid traffic-light color")
body:
fun main() {
val trafficLightColor = "Black"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
- Reassign the
trafficLightColor
variable to a"Yellow"
value:
fun main() {
val trafficLightColor = "Yellow"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
When you run this program, what do you think the output will be?
- Run the program and then view the output:
Slow
The output is a Slow
message because:
- The
trafficLightColor
variable is assigned a"Yellow"
value. - The program evaluates each condition one-by-one in sequence.
- The
"Yellow"
value isn't equal to the"Red"
value, so the program skips the first body. - The
"Yellow"
value is equal to the"Yellow"
value, so the program executes the second body and prints aSlow
message. - A body was executed, so the program ignores the third and fourth branches, and leaves the
when
statement.
Write more complex conditions in a when
statement
So far you learned how to write when
conditions for a single equal condition, such as when the trafficLightColor
variable is assigned a "Yellow"
value. Next, you learn to use the comma (,
), the in
keyword, and the is
keyword to form more complex when
conditions.
Build a program that determines whether a number between 1 and 10 is a prime number:
- Open Kotlin playground in a separate window.
You return to the traffic-light program later.
- Define an
x
variable and then assign it a3
value:
fun main() {
val x = 3
}
- Add a
when
statement that includes multiple branches for2
,3
,5
and7
conditions and follow each with aprintln("x is prime number between 1 and 10.")
body:
fun main() {
val x = 3
when (x) {
2 -> println("x is a prime number between 1 and 10.")
3 -> println("x is a prime number between 1 and 10.")
5 -> println("x is a prime number between 1 and 10.")
7 -> println("x is a prime number between 1 and 10.")
}
}
- Add an
else
branch with aprintln("x is not prime number between 1 and 10.")
body:
fun main() {
val x = 3
when (x) {
2 -> println("x is a prime number between 1 and 10.")
3 -> println("x is a prime number between 1 and 10.")
5 -> println("x is a prime number between 1 and 10.")
7 -> println("x is a prime number between 1 and 10.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- Run the program and then verify that the output is as expected:
x is a prime number between 1 and 10.
Use a comma (,
) for multiple conditions
The prime-number program contains a lot of repetition of println()
statements. When you write a when
statement, you can use a comma (,
) to denote multiple conditions that correspond to the same body.
In the previous diagram, if either the first or second condition is fulfilled, the corresponding body is executed.
Rewrite the prime number program with this concept:
- In the branch for the
2
condition, add3
,5
and7
separated by commas (,
):
fun main() {
val x = 3
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
3 -> println("x is a prime number between 1 and 10.")
5 -> println("x is a prime number between 1 and 10.")
7 -> println("x is a prime number between 1 and 10.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- Remove the individual branches for the
3
,5
and7
conditions:
fun main() {
val x = 3
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- Run the program and then verify that the output is as expected:
x is a prime number between 1 and 10.
Use the in
keyword for a range of conditions
Besides the comma (,
) symbol to denote multiple conditions, you can also use the in
keyword and a range of values in when
branches.
To use a range of values, add a number that denotes the start of the range followed by two periods without any spaces and then close it with another number that denotes the end of the range.
When the value of the parameter is equal to any value in the range between start of the range and the end of the range, the first body executes.
In your prime-number program, can you print a message if the number is between 1 and 10, but not a prime number?
Add another branch with the in
keyword:
- After the first branch of the
when
statement, add a second branch with thein
keyword followed by a1..10
range and aprintln("x is a number between 1 and 10, but not a prime number.")
body:
fun main() {
val x = 3
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- Change the
x
variable to a4
value:
fun main() {
val x = 4
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- Run the program and then verify the output:
x is a number between 1 and 10, but not a prime number.
The program prints the message of the second branch, but not the message of the first or third branch.
Here's how this program works:
- The
x
variable is assigned a4
value. - The program proceeds to evaluate conditions for the first branch. The
4
value isn't the2
,3
,5
, or7
values, so the program skips the execution of the body of the first branch and proceeds to the second branch. - The
4
value is between1
and10
, so the message ofx is a number between 1 and 10, but not a prime number.
body is printed. - One body is executed, so the program proceeds to leave the
when
statement and ignores theelse
branch.
Use the is
keyword to check data type
You can use the is
keyword as a condition to check the data type of an evaluated value.
In the previous diagram, if the value of the argument is of the data type stated, the first body is executed.
In your prime-number program, can you print a message if the input is an integer number that's outside the range of 1 to 10?
Add another branch with the is
keyword:
- Modify
x
to be of typeAny
. This is to indicate thatx
can be of value other thanInt
type.
fun main() {
val x: Any = 4
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- After the second branch of the
when
statement, add theis
keyword and anInt
data type with aprintln("x is an integer number, but not between 1 and 10.")
body:
fun main() {
val x: Any = 4
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
is Int -> println("x is an integer number, but not between 1 and 10.")
else -> println("x isn't a prime number between 1 and 10.")
}
}
- In the
else
branch, change the body to aprintln("x isn't an integer number.")
body:
fun main() {
val x: Any = 4
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
is Int -> println("x is an integer number, but not between 1 and 10.")
else -> println("x isn't an integer number.")
}
}
- Change the
x
variable to a20
value:
fun main() {
val x: Any = 20
when (x) {
2, 3, 5, 7 -> println("x is a prime number between 1 and 10.")
in 1..10 -> println("x is a number between 1 and 10, but not a prime number.")
is Int -> println("x is an integer number, but not between 1 and 10.")
else -> println("x isn't an integer number.")
}
}
- Run the program and then verify the output:
x is an integer number, but not between 1 and 10.
The program prints the message of the third branch, but not the messages of the first, second, or fourth branches.
Here's how the program works:
- The
x
variable is assigned a20
value. - The program proceeds to evaluate conditions for the first branch. The
20
value isn't the2
,3
,5
or7
values, so the program skips the execution of the body of the first branch and proceeds to the second branch. - The
20
value isn't inside the1
to10
range, so the program skips the execution of the body of the second branch and proceeds to the third branch. - The
20
value is ofInt
type, so thex is an integer number, but not between 1 and 10
body is printed. - One body is executed, so the program proceeds to leave the
when
statement and ignores theelse
branch.
Try it
Now practice what you learned in your traffic-light program.
Imagine that there's an amber traffic-light color in some countries that warns drivers the same way as a yellow traffic light in other countries. Can you modify the program so that this additional condition is covered and maintain the original conditions?
Add an additional condition with the same body
Add an additional condition to the traffic-light program:
- If you still have it open, go back to the instance of Kotlin Playground with your traffic-light program.
- If you closed it, open a new instance of Kotlin Playground and enter this code:
fun main() {
val trafficLightColor = "Yellow"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
- In the second branch of the
when
statement, add a comma after the"Yellow"
condition and then an"Amber"
condition:
fun main() {
val trafficLightColor = "Yellow"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow", "Amber" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
- Change the
trafficLightColor
variable to an"Amber"
value:
fun main() {
val trafficLightColor = "Amber"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow", "Amber" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
- Run this program and then verify the output:
Slow
4. Use if/else and when as expressions
You learned how to use if/else
and when
as statements. When you use conditionals as statements, you let each branch execute different actions in the body based on the conditions.
You can also use conditionals as expressions to return different values for each branch of condition. When the body of each branch appears similar, you can use conditional expressions to improve code readability compared to conditional statements.
The syntax for conditionals as expressions is similar to statements, but the last line of bodies in each branch need to return a value or an expression, and the conditionals are assigned to a variable.
If the bodies only contain a return value or expression, you can remove the curly braces to make the code more concise.
In the next section, you take a look at if/else
expressions through the traffic-light program.
Convert an if
statement to an expression
There's a lot of println()
statement repetition in this if/else
statement:
fun main() {
val trafficLightColor = "Black"
if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else if (trafficLightColor == "Green") {
println("Go")
} else {
println("Invalid traffic-light color")
}
}
Convert this if/else
statement to an if/else
expression and remove this repetition:
- In Kotlin playground, enter the previous traffic-light program.
- Define a
message
variable and then assign it anif/else
statement:
fun main() {
val trafficLightColor = "Black"
val message = if (trafficLightColor == "Red") {
println("Stop")
} else if (trafficLightColor == "Yellow") {
println("Slow")
} else if (trafficLightColor == "Green") {
println("Go")
} else {
println("Invalid traffic-light color")
}
}
- Remove all
println()
statements and their curly braces, but leave the values inside them:
fun main() {
val trafficLightColor = "Black"
val message =
if (trafficLightColor == "Red") "Stop"
else if (trafficLightColor == "Yellow") "Slow"
else if (trafficLightColor == "Green") "Go"
else "Invalid traffic-light color"
}
- Add a
println()
statement to the end of the program and then pass it themessage
variable as an argument:
fun main() {
val trafficLightColor = "Black"
val message =
if (trafficLightColor == "Red") "Stop"
else if (trafficLightColor == "Yellow") "Slow"
else if (trafficLightColor == "Green") "Go"
else "Invalid traffic-light color"
println(message)
}
- Run this program and then view the output:
Invalid traffic-light color
Try it
Convert the traffic-light program to use a when
expression instead of a when
statement:
- In Kotlin Playground, enter this code:
fun main() {
val trafficLightColor = "Amber"
when (trafficLightColor) {
"Red" -> println("Stop")
"Yellow", "Amber" -> println("Slow")
"Green" -> println("Go")
else -> println("Invalid traffic-light color")
}
}
Can you convert the when
statement to an expression so that you don't repeat the println()
statements?
- Create a
message
variable and assign it to thewhen
expression:
fun main() {
val trafficLightColor = "Amber"
val message = when(trafficLightColor) {
"Red" -> "Stop"
"Yellow", "Amber" -> "Slow"
"Green" -> "Go"
else -> "Invalid traffic-light color"
}
}
- Add a
println()
statement as the last line of the program and then pass it themessage
variable as an argument:
fun main() {
val trafficLightColor = "Amber"
val message = when(trafficLightColor) {
"Red" -> "Stop"
"Yellow", "Amber" -> "Slow"
"Green" -> "Go"
else -> "Invalid traffic-light color"
}
println(message)
}
5. Conclusion
Congratulations! You learned about conditionals and how to write them in Kotlin.
Summary
- In Kotlin, branching can be achieved with
if/else
orwhen
conditionals. - The body of an
if
branch in anif/else
conditional is only executed when the boolean expression inside theif
branch condition returns atrue
value. - Subsequent
else if
branches in anif/else
conditional get executed only when previousif
orelse if
branches returnfalse
values. - The final
else
branch in anif/else
conditional only gets executed when all previousif
orelse if
branches returnfalse
values. - It's recommended to use the
when
conditional to replace anif/else
conditional when there are more than two branches. - You can write more complex conditions in
when
conditionals with the comma (,
),in
ranges, and theis
keyword. if/else
andwhen
conditionals can work as either statements or expressions.