Operator when
Contents
- 1. Operator when. The purpose. Declaration syntax
- 2. Using the when operator with various objects. Examples
- 2.1. Determining the name of the day of the week
- 2.2. Determining the time of year by the name of the month. Batch processing in the when operator
- 2.3. Operator when without arguments. Using the when operator as part of a conditional expression. Example
- 2.4. Example. Using complex expressions in the statement when
- 2.5. Using a string type as an argument
- Related topics
Search other resources:
1. Operator when. The purpose. Declaration syntax
The when operator is used to implement the selection of one option from several options. The when operator in the Kotlin language is analogous to the switch operator in Java, C#, C/C++, only with broader capabilities.
The when operator allows you to operate on arbitrary objects, unlike the switch operator of other languages, which only used constants. The when operator is effective when the number of variants (condition checks) is three or more.
The syntax for declaring the when operator is varied and, in the most general case, is as follows:
when (name) { value1 -> statements_1 value2 -> statements_2 ... valueN -> statements_N else -> statements_N+1 }
here
- name – the name of the argument that defines the list of possible values value1, value2, …, valueN;
- value1, value2, valueN – the values that the variable name receives. These values can be separate literals, ranges (intervals), variables. It is also allowed to insert function calls that return a value of the same type as the variable name;
- statement_1, statement_2, statement_N – one or more statements that need to be executed if name matches respectively with value1, value2, valueN. If you need to execute two or more operators, then they are taken in curly braces {};
- statement_N+1 – one or more statements that are executed when no match is found between name and value1, value2, valueN.
The when operator works as follows. First, name is compared with value1. If the values are equal, then the statement (statement block) statement1 is executed and then the transition to the next statement following the when statement occurs. If the value of name and value1 do not match, then value2 is considered, which corresponds to the statement (several statements) statement2. If no matches are found for N values during equality tests, then statement_N+1 is executed.
⇑
2. Using the when operator with various objects. Examples
2.1. Determining the name of the day of the week
Task. The number of the day of the week is set (1..7). Use this number to display the name of the day of the week. Assume that Monday is number 1.
Solution.
fun main(args:Array<String>) { val day : Int print("day = ") day = readLine().toString().toInt() when (day) { 1 -> println("Monday") 2 -> println("Tuesday") 3 -> println("Wednesday") 4 -> println("Thirsday") 5 -> println("Friday") 6 -> println("Saturday") 7 -> println("Sunday") else -> println("Incorrect input.") } }
Test example
day = 3 Wednesday
⇑
2.2. Determining the time of year by the name of the month. Batch processing in the when operator
The example shows that when the operator can be applied to a group of elements and ranges.
Task. By the entered month number, display the name of the season to which this month belongs.
Solution.
fun main(args:Array<String>) { // 1. Declaring a variable val month : Int // 2. Input month number print("month = ") month = readLine().toString().toInt() // 3. Calculation. Display the name of the season when (month) { 1, 2, 12 -> println("Winter") in 3..5 -> println("Spring") // the range in 6..8 -> println("Summer") in 9..11 -> println("Autumn") else -> println("Incorrect input.") } }
Test example
month = 6 Summer
⇑
2.3. Operator when without arguments. Using the when operator as part of a conditional expression. Example
The when operator can be used without arguments. The following example uses a when operator without arguments as part of a conditional expression.
Example. Develop a program that determines leap-year or not.
fun main(args:Array<String>) { // Operator when without arguments. Definition of leap-year // 1. Declare variables val year : Int // 2. Input year print("year = ") year = readLine().toString().toInt() // 3. Checking for correct input if (year < 0) { println("Incorrect input.") return } // 4. Calculation // Conditional expression + when operator val leap_year : Boolean = when { year % 400 == 0 -> true year % 100 == 0 -> false year % 4 == 0 -> true else -> false } // 5. Display the result if (leap_year) println("Leap year") else println("Not a leap year") }
Test example
year = 998400 Leap year
⇑
2.4. Example. Using complex expressions in the statement when
Task.
From the beginning of 1990 to a certain day n months and 2 days have passed. Determine the name of the month of this day (January, February, etc.).
Solution. Since the condition of the problem says “From the beginning of 1990 …”, this means that it will be January 1, 1990. In accordance with this, the task is solved.
fun main(args:Array<String>) { // Operator when. Determining the month of the year // 1. Declare variables val n : Int // 2. Input the number of months n print("n = ") n = readLine().toString().toInt() // 3. Checking for correct input if (n < 0) { println("Incorrect input.") return } // 4. Calculation // Conditional expression, the argument is an expression val month = when (n % 12) { 0 -> println("January") 1 -> println("February") 2 -> println("March") 3 -> println("April") 4 -> println("May") 5 -> println("June") 6 -> println("July") 7 -> println("August") 8 -> println("September") 9 -> println("October") 10 -> println("December") 11 -> println("November") else -> println("Incorrect input.") } }
Testing example
n = 3 April
⇑
2.5. Using a string type as an argument
Task.
Full names of the days of the week are specified, starting with a capital letter. Determine their serial number. Assume that Monday is number 1.
Solution.
fun main(args:Array<String>) { // Operator when. The argument is the type String // 1. Declaring variables val day : String // 2. Input the name of the day of the week print("day = ") day = readLine().toString() // 3. Calculation // Operator when, the argument id String type val num_day = when(day){ "Monday" -> 1 "Tuesday" -> 2 "Wednesday" -> 3 "Thirsday" -> 4 "Friday" -> 5 "Saturday" -> 6 "Sunday" -> 7 else -> "Incorrect input." } // 4. Display the number of day println("num_day = " + num_day) }
Test example
day = Thirsday num_day = 4
⇑
Related topics
- The conditional branch operator if-else. Types of conditional branch operators
- Nested conditional branch operators. The if condition statement as part of other statements. Examples
- Conditional expression. Concepts. Features of use with the if and when statements. Examples
⇑