31
loading...
This website collects cookies to deliver better user experience
if
and when
statements.condition
must returns boolean result.if (condition) {
// code..
}
fun main() {
// create number variable
val number = 12
// check if number is an even number
if (number % 2 == 0) {
// if true, execute this code
println("even number")
}
}
even number
number
variable is compared using ==
operator to check if the result of number % 2
is equals 0
. If the result is true, then the code inside if
block is executed.Operator | Description |
---|---|
== |
Equals to |
!= |
Not equals to |
> |
Greater than |
>= |
Greater than or equals |
< |
Less than |
<= |
Less than or equals |
&&
) and OR (||
) is available.Condition | Condition | Result |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Condition | Condition | Result |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
if
statement can be added with else
statement to execute certain code if the condition inside if
is not match. This is the basic syntax of using if else
statement.if(condition) {
// code..
} else {
// code..
}
if else
statement.fun main() {
// create name variable
val name = "firstname"
// check if name's value is empty
if (name.isEmpty()) {
println("name is empty!")
} else {
println("Hello, $name !")
}
}
Hello, firstname !
if else
statement is used to check if the value from name
variable is empty by using name.isEmpty()
. Because the value of name
variable is not empty, the code inside else
block is executed.if else if
statement. This statement is suitable to select many conditions.score
is checked to get the score's predicate.fun main() {
// create score variable
val score = 78
// check the score's value to get the score's predicate
if (score >= 90) {
println("Excellent")
} else if (score < 90 && score >= 75) {
println("Very Good")
} else if(score < 75 && score >= 55) {
println("Good")
} else if(score < 55) {
println("Bad")
} else {
println("Very Bad")
}
}
Very Good
if else if
is used to check the value from score
variable to get the score's predicate.in
keyword followed with range specification.// range from 1 until 10
score in 1..10
score
variable.fun main() {
// create score variable
val score = 78
// check the score's value to get the score's predicate
if (score >= 90) {
println("Excellent")
} else if (score in 75..89) {
println("Very Good")
} else if(score in 55..74) {
println("Good")
} else if(score < 55) {
println("Bad")
} else {
println("Very Bad")
}
}
Very Good
if
.fun main() {
val email = "[email protected]"
val password = "secret"
if(email.equals("[email protected]")) {
println("Email is valid")
if (password.equals("secret")) {
println("Password is valid")
}
} else {
println("Email or password is invalid!")
}
}
Email is valid
Password is valid
if
is executed first then the inner if
is executed.when
statement is similiar with switch case
condition selection. This is the basic syntax of using when
statement.when(variable_to_be_checked) {
condition -> // single code..
condition -> {
// multiple line of codes with curly braces.
}
..
else -> // code for "default" condition..
}
when
statement is used to get the job's description from job
variable.fun main() {
val job = "Front End Developer"
when(job) {
"Front End Developer" -> println("Develop Front End App")
"Back End Developer" -> {
println("Develop Back End App")
println("Having Fun with server")
}
"Mobile Developer" -> println("Develop Mobile App")
else -> println("Just Do It")
}
}
Develop Front End App
when
statement is using job
variable to check the variable's value that suitable with the specified condition. Because the value of job
variable is equals to Front End Developer
, the code is executed.if
statement is suitable for complex condition, for example a range condition.when
statement is suitable for simple condition with multiple cases.