syntax:
if(condition){
//statement 1
}else {
//statement 2
}
If condition is true, perform statement 1 but if false, perform statement 2. Take note that ternary operator (condition ? value1 : value2) is not use in here since ternary operator works just like if. Also if "if" is used in an expression, the last statement is the considered as the return value even if you did not used the keyword return.
syntax:
val name1 = if(condition){
//statement 1
// last statement 1
}else {
// statement 2
// last statement 2
}
We created a variable whose value depends on an if statement. If the condition is true, the last statement 1 is the value else if false, the last statement 2 is the value.
====================
fun main(){
val x = 6
val y = 9
val answer = if(x > y){
x - y
}else {
y - x
}
println("answer: $answer")
}
===================
Result:
answer: 3
YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
