Skip to content

Kotlin If, When and While

Maria Maged M edited this page Apr 12, 2020 · 4 revisions

Kotlin - If, When and While

  • if is nearly ubiquitous in computer programming.
Java
if(i > 10) {
	// Do something.
}
else {
	// Do something else.
}
JavaScript
if(i > 10) {
	// Do something.
}
else {
	// Do something else.	
}
// Yes, it is the same as Java syntax.
Ruby
if(i > 10) 
	# Do something.
else
	# Do something.
end	
Kotlin
val i = 3
if(i > 10) {
	println("Something")
}
else {
	println("Something else")
}
  1. For single-line conditional work, you can skip the braces.
val i = 3
if(i > 10) println("Something") else println("Something else")
  1. It also supports else if in addition to a simple else:
val i = 3
if(i > 10) println("Something")
else if(i > 2) println("Something else")
else println("And now for something completely different")

Clone this wiki locally