If Statement in Kotlin
If Statement Definition
- In Kotlin, If conditional statement executes the block of statements based on the condition inside the braces similar to other programming languages. If the condition is true, then the block of code inside the if the block is executed otherwise the if block is skipped.
- IF conditional statement is a feature of the programming language which performs different computations or operations depending on whether a programmer-specified boolean condition evaluates to true or false.
Syntax
if(condition){
//Block of code
}
For example,
var num = 5
var maximum = 10
if(num>maximum){
maximum = num
}
(OR)
var num = 5
var maximum = 10
if(num>maximum) maximum = num
If Statement Example Program
fun main(args: Array<String>){
//Assigning a constant value to a variable
val num = 5
//Assigning a value to a varying variable
var maximum = 10
if(num > maximum){
maximum = num
}
println("The maximum value is : $maximum")
}
Sample Output
The maximum value is : 10
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
- Float Data type Usage and Type Conversion in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Elvis Operator (?:) in Kotlin
- Not Null Assertion(!!) Operator in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Safe Call Operator (?.) in Kotlin
- Logical Operators in Kotlin
- Read Data Input from Command Line in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Kotlin Operators Overview
- Repeat and Its Usage in Kotlin
- Int Data type Usage and Type Conversion in Kotlin
- Read String Data Input in Kotlin
- First Program in Kotlin