When Statements

Contents

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
23m
Students
29
Ratings
5/5
starstarstarstarstar
Description

In this course, you will learn about control flow statements in Kotlin, including the if-else statement, the if-else-if ladder statement, the nested if statement, and when statements.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Transcript

Well, hello everyone. So, in this video we're going to learn how to use the when statement and understand how to control the flow of our program's execution. So, the when statement executes one statement from multiple conditions. You might think it's like the if-else-if ladder statement that you learned in the previous video. The when statement works with data types such as byte, short, int, long, and string. As you can see in the diagram, when the statement starts with when, which is a keyword and a conditional expression between brackets. If Condition 1 is true, Code Block 1 gets executed. If Condition 1 is false and case Condition 2 is true, Code Block 2 is executed. If Condition 1 is false and Condition 2 is false, if Condition n is true, Code Block n is executed. Now, if all case conditions are false, the codes in the else block are executed. Now, there are some important points when you're using this when statement. So, let's consider them.

There can be one or n number of case values for a when expression. The case value must be of when expression type only. And the case value must be literal or constant. We cannot use variables here. So, let's make some examples and we'll use some when statements. First, I'm just going to create a new common file, so I'll right click on the package name, select the 'New', 'Kotlin Class/File' option. After selecting the file option here, I'll determine the file name and I'll just call it WhenStatement, hit 'enter'. So, now I've got to create the main method. Alright, main and select the 'maina' option, here and 'enter'. Okay, so now we've got the main method created. Let's do up our example like this. We'll ask the user to enter a number between 1 and 7 into the console. So, we'll print the day of the week to the console, according to the number entered by the user. For example, if the user enters the number 1 we'll print Monday to the console. If they enter the number 7 we'll print Sunday on the console and everything in between. So, let's get right into it.

First up, we'll prepare a message that informs a user. Let's write, print("Please enter a day number of week:"). Now, I'll create a variable of the type integer called dayNumber. Then I'll pass the value entered in the console to this variable. Now, let's create a variable of the type string. But we cannot initialize this variable. So, we're going to pass the days of the week to this variable. Now, let's create the when statement. So, I type when and then select the 'when' option suggested by the editor. I'll write the dayNumber variable inside parenthesis because we have to pass the value entered in the console to this variable. Now, let's create the conditions. So, I'm writing 1 ->. This expression is called the lambda. After the lambda, we must define the operation that needs to be done. If the user enters a number 1 in the console, we should print Monday to the console. So for this, I'll pass the value Monday to the day variable and that's how we've created our first condition.

Now, we have to create the other seven conditions. So, for this I just need to copy and paste this condition seven times. Then if the user enters the number 2, let's make it Tuesday. If the user enters number 3, it'll be Wednesday. Enters 4, it'll be Thursday, 5 it'll be Friday, 6 Saturday, and finally, if they enter the number 7, it will be Sunday. Now, if the user enters a different number, we've got to create that else condition for that scenario. So, in this case, we can pass the following expression to the day variable, "Invalid day choice". Now, why don't we go ahead and print the day variable to the console outside of the when statement. So, after typing sout, I'll press 'enter' and I'll write, println("Day is $day").

Okay, so now let's run the code. Enter '3' for the day number of the week. Case condition 3 is executed and "Day is Wednesday" is the message that gets displayed. So, let's run the code again, enter '5'. Case condition 5 is executed and "Day is Friday" is the message that gets displayed. Let's see what happens when we run the code and enter '8'? See, so the default part of the when statement gets executed and the invalid day choice message gets displayed. So, my friends now you get a pretty good understanding of how that when statement gets used. So, why don't we just take a short break here and then in the next lesson we're going to learn some loops. So, I'll see you on the other side.

 

About the Author
Students
456
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics