MOCKSTACKS
EN
Questions And Answers

More Tutorials








Kotlin If..Else..Else If decision making

Kotlin supports below decision making statements:

  • “If” statements: where if a condition is true it is used to specify execution for a block of code.
  • “Else” statements: where if the same condition is false it specifies the execution for a block of code.
  • “Else if” statements: this specifies a new test if the first condition is false.

Syntax of If statement

if (condition) {
  // block of code to be executed if the condition is true
} else if (another condition){
 // code to be executed for another condition
} else if (another condition){
 // code to be executed for another condition
} else if (another condition){
 // code to be executed for another condition
} else {
 // default code execution if not met in above statements
}

Example of if, Else, Else if in Kotlin

fun main() {
  val time = 22
  if (time < 10) {
    println("Good morning.")
  } else if (time < 20) {
    println("Good day.")
  } else {
    println("Good evening.")
  }
}

Output

Good morning.

Conclusion

In this page (written and validated by ) you learned about Kotlin If..Else..Else If decision making . What's Next? If you are interested in completing Kotlin tutorial, your next topic will be learning about: Kotlin Switch Statement When equivalent.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.