Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn kotlin If Else


Certainly! The if-else statement in Kotlin is used for conditional execution of code blocks. It allows you to execute different blocks of code based on whether a condition is true or false. Here's a simple explanation with an example:

Syntax

Example

if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}

Example

Let's say we want to check if a person is eligible to vote based on their age:

fun main() {
val age = 18

if (age >= 18) {
println("You are eligible to vote")
} else {
println("You are not eligible to vote")
}
}

OutPut

You are eligible to vote

Explanation

Nested if-else Statements

You can also nest if-else statements within each other for more complex conditions.

Example

fun main() {
val score = 75

if (score >= 90) {
println("You got an A")
} else if (score >= 80) {
println("You got a B")
} else if (score >= 70) {
println("You got a C")
} else {
println("You need to improve")
}
}

OutPut

You got a C

Conclusion

The if-else statement is a fundamental control flow construct in Kotlin that allows you to execute different blocks of code based on conditions. It's commonly used for decision-making in programs, enabling you to create more dynamic and flexible code.

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java