Conditional branching is for executing a specific set of code based on the condition satisfied.
For conditional branching in kotlin we have two statements namely
1. IF
2. WHEN
1. IF
Syntax:
if (condition) {
//code to be executed
}
- Keyword “if” followed by condition and the code to be executed goes under curly braces {}
Example 1:
fun main (args:Array<String>)
{
var x = 10
var y = 20
if(x > y){
println("x is greater than y")
}
}
Explanation
- Two variables x and y are compared using a comparison operator*
- if comparison returns true, code gets executed
- As the condition fails, nothing gets printed
* Wondering about different types of operators in Kotlin? click here to know more
Note:
If you have only one line of code you can omit the curly braces
Example 2
“if” can be used as an expression to return a value
fun main (args:Array<String>)
{
var x = 5
print( if (x < 3) "x is smaller" else "x is bigger" )
}
Output
x is bigger
Explanation
- The condition x < 3 is evaluated, as “x” is assigned the value 5 “else” part gets executed and prints “x is bigger”.
Example 3
While using “if” as an expression, “else” part becomes mandatory
fun main (args:Array<String>)
{
var x = 5
print( if (x < 3) "x is smaller")
}
Output
Kotlin: ‘if’ must have both main and ‘else’ branches if used as an expression
Explanation
- “If” used as an expression, “else” part becomes mandatory so the error.
2. IF..ELSE
Syntax:
if (condition) {
// if condition is satisfied
} else{
// if condition is not satisfied
}
- Keyword “if” followed by a condition
- condition satisfied -> “if” block gets executed otherwise “else” block gets executed
Example:
fun main (args:Array<String>)
{
var x = 10
var y = 20
if(x > y){
println("x is greater than y")
}
else{
println("y is greater than x")
}
}
Output
y is greater than x
Explanation
- We are comparing two variables x and y using a comparison operator
- In our example variable x is lesser than y, as a result “else” block gets executed and “y is greater than x” gets printed
3. WHEN
Syntax:
when (condition) {
case 1 -> code for case
case 2 -> code for case
else -> {
//code if no condition satisfies
}
}
- Keyword “when” followed by a condition and in the curly braces { } we mention different cases
- Whichever condition satisfies code under that would be executed, if none matches else block gets executed
Example 1
fun main (args:Array<String>)
{
var x = true
when (x) {
true -> print("x is set to true")
false -> print("x is set to false")
}
}
Output
x is set to true
Explanation
- As x is set to true, case “true” gets executed and as a result prints “x is set to true”.
Example 2
Conditions can be combined by separating them with a comma
fun main (args:Array<String>)
{
var x = 10
when (x) {
10, 20 -> print("x == 10 or x == 20")
else -> print("otherwise")
}
}
Output
x == 10 or x == 20
Explanation
- As “x” is set to 10 the first case satisfies and code under it gets executed
Example 3
Range of the value can be checked using the keyword “in“
fun main (args:Array<String>)
{
var x = 30
when (x) {
in 1..10 -> print("In the range")
!in 1..10 -> print("Outside the range")
else -> print("none of the above")
}
}
Output
Outside the range
Explanation
- Range is checked by using “in” not in the range is checked using “!in”
- As variable x is assigned the value 30, not in the range condition satisfies.
Example 4
fun main (args:Array<String>)
{
var validNumbers = arrayOf(10,20,30,40)
var x = 20
when (x) {
in validNumbers -> print("x is valid")
else -> print("none of the above")
}
}
Output
x is valid
Explanation
- Variable “x” is assigned the value 20
- array “validNumbers” is declared and values are defined under it
- using “in” keyword condition is checked, if “x” falls under the values defined in the array
- as condition satisfies, “x is valid” gets printed
Example 5
Type of value can be checked using the keyword “is”
fun main (args:Array<String>)
{
var x = "Kotlin Tutorials"
when(x)
{
is String -> print("x of type String")
else -> print("x is not of type String")
}
}
Output
x of type String
Explanation
- As variable x is assigned a string value the first condition satisfies and prints “x is of type String”
Example 6
“is” can be negated “!is” and used for checking not of type
However if the variable defined is of one type and the cases mentioned are of other type, the compiler throws error
fun main (args:Array<String>)
{
var x = "Kotlin Tutorials"
when(x)
{
is Int -> print("x of type Int")
!is Int -> print("x is not of type Int")
else -> print("x is not of type Others")
}
}
Output
Compiler error – Incompatible types: Int and String
Explanation
- variable “x” is assigned a string value
- while doing type check using the keyword “is”, Int is mentioned as type
- as the types compared are of different types, compiler throws an error
Example 7
Used as replacement for “if..else if”
fun main (args:Array<String>)
{
var x = false
var y = false
var z = true
when {
x -> print("x is set to true")
y -> print("y is set to true")
z -> print("z is set to true")
else -> print("neither is set to true")
}
}
Output
z is set to true
Explanation
- Among the variables as z is set to true, “case z” get’s executed and prints “z is set to true”