If else Statement in Kotlin
Definition
The if statement is used to check a condition. If the condition mentioned in the if the condition is true, then the block of code inside the if block is executed, otherwise the block of code inside the else part is executed.
Syntax
if(condition){
//Block of code
}else{
//Block of code
}
If..else statement
These expressions can also be written as blocks as,
var num = 5
var maximum = 10
val max = if(num>maximum){
num
} else {
maximum
}
In the above statements, the condition is checked and the maximum value is assigned to the "max" variable.
if else Kotlin example program
// If Else Statement Kotlin example program
// Conditional Statements Kotlin Programs, Basic Kotlin Programs
fun main(args: Array < String > ) {
val num = 5 // num = 10
// If Condition Statement
if (num == 5) {
// If Block
println("Condition of $num equal to 5 is : True ")
} else {
// Else Block
println("Condition of $num equal to 5 is : False ")
}
}
Sample output
//If num = 5
Condition of 5 equal to 5 is : True
//If num = 10
Condition of 10 equal to 5 is : True
Read More Articles
- Read Data Input using Scanner in Kotlin
- Declare Variables In Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Is Operator (is and !is) in Kotlin
- Read Data Input from Command Line in Kotlin
- Read String Data Input in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Elvis Operator (?:) in Kotlin
- Not Null Assertion(!!) Operator in Kotlin
- Logical Operators in Kotlin
- Safe Call Operator (?.) in Kotlin
- Repeat and Its Usage in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin