Search this site
Embedded Files
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
  • More
    • Home
    • Institute Coaching Programs
    • Android curriculum
      • Android Topics
        • Notification
        • Broadcast & Broadcast Receiver
        • Content Providers
        • Activities
          • Activity and Lifecycle
          • Saving state
          • Tasks and the back stack
        • Services
          • Services
          • Foreground Service
          • Bound Service
        • Dependency Injection
        • Workmanager
        • Fragment Findings*
        • Navigation findings*
        • Permissions Findings*
        • Room & Datastore findings*
      • Kotlin Topics
        • Nullables
        • Exceptions
        • Conditions and Loops
        • Arrays
        • Collections
        • Collection Functions
        • Class & Object
        • OOP Concepts
        • Scope Function
        • Visibility Modifiers
        • Generics
        • Kotlin Classes Types
          • Enums
          • Sealed Class
          • Data Classes
          • Nested and inner classes
          • Object expressions and declarations
        • Kotlin Functions Types
          • Functions
          • Extension functions
          • Higher-order functions and lambdas
        • Couroutines
        • Kotlin Flows
      • Interview Questions
        • Android Interview Questions
    • Products
      • Shabri
      • WedExhibit
    • About

Conditions and Loops↗

If expression

In Kotlin, if is an expression: it returns a value. Therefore, there is no ternary operator (condition ? then : else) because ordinary if works fine in this role.



Branches of an if expression can be blocks. In this case, the last expression is the value of a block:

val max = if (a > b) {

   print("Choose a")

   a

} else {

   print("Choose b")

   b

}


When Expression↗️

when defines a conditional expression with multiple branches. It is similar to the switch statement in C-like languages. Its simple form looks like this.


when matches its argument against all branches sequentially until some branch condition is satisfied.

when can be used either as an expression or as a statement.

If when is used as an expression, the else branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions, for example, with enum class entries and sealed class subtypes).

To define a common behavior for multiple cases, combine their conditions in a single line with a comma:

You can also check a value for being in or !in a range or a collection:

when can also be used as a replacement for an if-else if chain. If no argument is supplied, the branch conditions are simply boolean expressions, and a branch is executed when its condition is true:

For loops↗️

for (item in collection) print(item)


  • To iterate over a range of numbers, use a range expression

  • A for loop over a range or an array is compiled to an index-based loop that does not create an iterator object.

  • If you want to iterate through an array or a list with an index, you can do it this way

  • Alternatively, you can use the withIndex library function:

while and do-while loops execute their body continuously while their condition is satisfied. The difference between them is the condition checking time:

  • while checks the condition and, if it's satisfied, executes the body and then returns to the condition check.

  • do-while executes the body and then checks the condition. If it's satisfied, the loop repeats. So, the body of do-while executes at least once regardless of the condition.

Kotlin supports traditional break and continue operators in loops

A brand by Jagrut Soni
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse