- Kotlin Tutorial
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
- Kotlin Useful Resources
- Kotlin - Quick Guide
- Kotlin - Useful Resources
- Kotlin - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Kotlin - if...else Expression
Kotlin if...else expressions works like an if...else expression in any other Modern Computer Programming. So let's start with our traditional if...else statement available in Kotlin.
Syntax
The syntax of a traditional if...else expression is as follows:
if (condition) { // code block A to be executed if condition is true } else { // code block B to be executed if condition is false }
Here if statement is executed and the given condition is checked. If this condition is evaluated to true then code block A is executed, otherwise program goes into else part and code block B is executed.
Example
You can try the following example:
fun main(args: Array<String>) { val age:Int = 10 if (age > 18) { print("Adult") } else { print("Minor") } }
When you run the above Kotlin program, it will generate the following output:
Minor
Kotlin if...else Expression
Kotlin if...else can also be used as an expression which returns a value and this value can be assigned to a variable. Below is a simple syntax of Kotlin if...else expression:
Syntax
val result = if (condition) { // code block A to be executed if condition is true } else { // code block B to be executed if condition is false }
If you're using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.
Examples
fun main(args: Array<String>) { val age:Int = 10 val result = if (age > 18) { "Adult" } else { "Minor" } println(result) }
When you run the above Kotlin program, it will generate the following output:
Minor
You can ommit the curly braces {} when if has only one statement:
fun main(args: Array<String>) { val age:Int = 10 val result = if (age > 18) "Adult" else "Minor" println(result) }
When you run the above Kotlin program, it will generate the following output:
Minor
You can include multiple statements in if...else block, in this case the last expression is returned as the value of the block. Try the following example:
fun main(args: Array<String>) { val age:Int = 10 val result = if (age > 18) { println("Given condition is true") "Adult" } else { println("Given condition is false") "Minor" } print("The value of result : ") println(result) }
When you run the above Kotlin program, it will generate the following output:
Given condition is false The value of result : Minor
Kotlin if...else...if Ladder
You can use else if condition to specify a new condition if the first condition is false.
Syntax
if (condition1) { // code block A to be executed if condition1 is true } else if (condition2) { // code block B to be executed if condition2 is true } else { // code block C to be executed if condition1 and condition2 are false }
Example
fun main(args: Array<String>) { val age:Int = 13 val result = if (age > 19) { "Adult" } else if ( age > 12 && age < 20 ){ "Teen" } else { "Minor" } print("The value of result : ") println(result) }
When you run the above Kotlin program, it will generate the following output:
The value of result : Teen
Kotlin Nested if Expression
Kotlin allows to put an if expression inside another if expression. This is called nested if expression
Syntax
if(condition1) { // code block A to be executed if condition1 is true if( (condition2) { // code block B to be executed if condition2 is true }else{ // code block C to be executed if condition2 is fals } } else { // code block D to be executed if condition1 is false }
Example
fun main(args: Array<String>) { val age:Int = 20 val result = if (age > 12) { if ( age > 12 && age < 20 ){ "Teen" }else{ "Adult" } } else { "Minor" } print("The value of result : ") println(result) }
When you run the above Kotlin program, it will generate the following output:
The value of result : Adult
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Kotlin if expression?
A - Kotlin support traditional if...else expression.
B - Kotlin if...else expression can be nested.
C - Kotlin if...else expression returns a value which can be assigned to a variable.
Answer : D
Explanation
All the mentioned statements are correct about Kotlin if expression.
Q 2 - Which of the following is not supported by Kotlin?
Answer : B
Explanation
Kotlin does not support if...then...else statement.
Q 3 - What will be the output of the following code?
fun main(args: Array<String>) { var x = 20 var y = 15 var z = "Mango" val result = if (x > y ) { z = "Orange" } else { z = "Apple" } println("Value of result = $z") }
Answer : B
Explanation
Correct answer is Orange, because x is greater than y, so once this condition is true, it will assign Orange to z variable.