In addition to the conditional structure “if”,  Kotlin gives us a conditional structure for situations we have to verify multiple conditions that are solved with “if” nested called WHEN Conditional structure.

By means of a series of examples we will see the syntax of the structure when.

Problem 1

Write a program that asks you to enter the coordinate of a point in the plane, ie two integer values x and y.
Then print on the screen where the quadrant is located using WHEN Conditional structure. (1) If y> 0 and y> 0, 2nd Quadrant: x <0 Y y> 0, 3º Quadrant: x <0 Y y <0 Quadrant: x> 0 Y y <0)
If any or both values are zero then the point is on an axis.

Project59 – Principal.kt

``````fun main (parameter: Array <String>) {
print ("Enter x coordinate of the point:")
val x = readLine () !!. toInt ()
print ("Enter coordinate and point:")
val y = readLine () !!. toInt ()
when {
x> 0 && y> 0 -> println ("First quadrate")
x <0 && y> 0 -> println ("Second quadrant")
x <0 && y <0 -> println ("Third quadrant")
x> 0 && y <0 -> println ("Fourth quadrant")
else -> println ("The dot is on an axis")
}
}`````` WHEN Conditional structure in Kotlin language

We have the keyword when and between braces the different conditions and then the operator -> the instruction (s) to execute if the condition is met .:

```     when {
x > 0 && y> 0 -> println ("First quadrate")
x <0 && y> 0 -> println ("Second quadrant")
x <0 && y <0 -> println ("Third quadrant")
x> 0 && y <0 -> println ("Fourth quadrant")
else -> println ("The dot is on an axis")
}
```

If any of the conditions are true, the following are not analyzed.

If none of the four conditions set in the when is true, the instructions that we have after the else are executed.

We can compare the same same problem solved with if nested and see that it is more concise and clear with when:

```     if (x> 0 && y> 0)
print ("Found in the first quadrant")
else
if (x <0 && y> 0)
print ("It is in the second quadrant")
else
if (x <0 && y <0)
print ("Found in the third quadrant")
else
if (x> 0 && y <0)
print ("Found in the fourth quadrant")
else
print ("Found on an axis")
```

Problem 2

Make a program that asks for three notes of a student by keyboard, calculate the average and print some of these messages using WHEN Conditional structure:
If the average is> = 7 show “Promoted”.
If the average is> = 4 and <7 show “Regular”.
If the average is <4 show “Failed”.

Project60 – Main.kt

``````fun main (Parameters: Array <String>) {
print ("Enter first note:")
val nota1 = readLine () !!. toInt ()
print ("Enter second note:")
val nota2 = readLine () !!. toInt ()
print ("Enter third note:")
val nota3 = readLine () !!. toInt ()
average value = (nota1 + nota2 + nota3) / 3
when {
average> = 7 -> print ("Promoted")
average> = 4 -> print ("Regular")
else -> print ("Free")
}
}``````

Problem 3

Carry out a program that allows you to enter the weight (in kilograms) of pieces. The process ends when we enter the value 0 using WHEN Conditional structure.
It should be reported:

A) How many pieces have a weight between 9.8 kg and 10.2 kg?, How many with more than 10.2 kg? And how many less than 9.8 kg?

B) The total number of pieces processed.

Project61 – Principal.kt

``````fun main (parameter: Array <String>) {
var cant1 = 0
var cant2 = 0
var cant3 = 0
do {
// coding180.com
print ("Enter part weight (0 to finish):")
val weight = readLine () !!. toDouble ()
when {
weight> 10.2 -> cant1 ++
weight> = 9.8 -> cant2 ++
weight> 0 -> cant3 ++
}
} while (weight! = 0.0)
println ("Fit parts: \$cant2")
println ("Parts greater than 10.2: \$cant1")
println ("Parts weighing less than 9.8: \$cant3");
val sum = cant1 + cant2 + cant3
println ("Total quantity of pieces processed: \$sum")
}``````

The else section is optional as we can see in this problem.

Structure when as expression

We saw that in Kotlin there is a possibility that the if conditional structure returns a value, the same possibility is presented with the when structure.

Problem 4

Enter the salaries of 10 employees per keyboard. Display a message according to the salary value using WHEN Conditional structure:

``` "High salary" if it is> 5000
"Average salary" if it is <= 5000 and> 2000
"Low salary" if it is <= 2000
```

Also show the cumulative total of expenses in high salaries.

Project62 – Principal.kt

``````fun main (parameter: Array <String>) {
// coding180.com
var total = 0
for (i in 1..10) {
print ("enter worker's salary:")
val salary = readLine () !!. toInt ()
total += when {
salary> 5000 -> {
println ("High salary")
salary
}
salary> 2000 -> {
println ("Average Salary")
0
}
else -> {
println ("Low salary")
0
}
}
}
println ("Total expenses in high salaries: \$total")
}``````

The structure when returns an integer value that we accumulate in the total variable. If you enter the first condition of the when we display the message “Salary high” and we return the value of the salary.

As we should only accumulate high wages when it is a medium or low salary we return the zero value that does not affect the accumulation.

Note that when we have two or more instructions after the -> operator we must set the open and closed keys.

Problems proposed

• Enter an integer value by keyboard, display a legend per screen that indicates whether the number is positive, null, or negative using WHEN Conditional structure.
• Propose a structure that is repeated 5 times and within the same load 3 integer values. Accumulate only the highest of each list of three values.
• Make a program that reads the sides of n triangles, and inform:
A) Of each of them, what type of triangle is: equilateral (three equal sides), isosceles (two equal sides), or scalene (no side equal)
B) Number of triangles of each type.

Solution

[sociallocker id=”958″]

``````Project63

fun main (parameter: Array <String>) {
//coding180.com
print ("Enter an integer value:")
val value = readLine () !!. toInt ()
when {
value == 0 -> println ("Zero entered")
value> 0 -> println ("A positive value was entered")
else -> println ("A negative value was entered")
}
}

Project64

fun main (parameter: Array <String>) {
//coding180.com
var sum = 0
for (i in 1..5) {
print ("Enter first value:")
val value1 = readLine () !!. toInt ()
print ("Enter second value:")
val value2 = readLine () !!. toInt ()
print ("Enter third value:")
val value3 = readLine () !!. toInt ()
sum += when {
value1> value2 && value1> value3 -> value1
value2> value3 -> value2
else -> value3
}
}
println ("The accumulated value of the greater of each list of 3 values ​​is: \$sum")
}

Project65

fun main (argument: Array <String>) {
//coding180.com
var cant1 = 0
var cant2 = 0
var cant3 = 0
print ("Enter the number of triangles:")
val n = readLine () !!. toInt ()
for (i in 1..n)
print ("Enter side 1:")
val side1 = readLine () !!. toInt ()
print ("Enter side 2:")
val side2 = readLine () !!. toInt ()
print ("Enter side 3:")
val side3 = readLine () !!. toInt ()
when {
side1 == side2 && side1 == side3 -> {
println ("It is an equilateral triangle.")
cant1 ++
}
side1 == side2 ||  Side1 == side3 ||  Side2 == side3 -> {
println ("It is an isosceles triangle.")
cant2 ++
}
else -> {
println ("It is a scalene triangle.")
cant3 ++
}
}
}
println ("Number of equilateral triangles: \$cant1")
println ("Number of isosceles triangles: \$cant2")
println ("Number of scalen triangles: \$cant3")
}``````

[/sociallocker]