Conditions and loops | Kotlin
- If문
val isEqual 5 == 3
val isNotEqual = 5 != 5
var heightPerson1 = 170
var heightPerson2 = 189
if (heightPerson1 > heightPerson2) {
println("use raw force")
} else if (heightPerson1 == heightPerson2) {
println("use your power technique 1337")
} else {
println("use technique")
}
val age = 17
if (age > 30)
println("you're over 30")
// println("you're over 30")
if (age >= 21) {
println("now you may drink in the US")
} else if (age >= 18) {
println("you may vote now")
} else if (age >= 16) {
println("you may drive now")
} else {
print("you're too young")
}
- 메모 - 식으로 된 If문
//create a variable for testing all condition
val age = 16
//create a variable for drinkingAge
val drinkingAge = 21
//create a variable for votingAge
val votingAge = 18
//create a variable for drivingAge
val drivingAge = 16
//Assign the if statement to a variable
val currentAge = if (age >=drinkingAge){
println("Now you may drink in the US")
//return the value for this block
drinkingAge
}else if(age >=votingAge){
println("You may vote now")
//return the value for this block
votingAge
}else if (age>=drivingAge){
println("You may drive now")
//return the value for this block
drivingAge
}else{
println("You are too young")
//return the value for this block
age
}
//print the age for the passing condition
print("current age is $currentAge")