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:
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.
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 it is used as an expression, the value of the first matching branch becomes the value of the overall expression. If it is used as a statement, the values of individual branches are ignored. Just like with if
, each branch can be a block, and its value is the value of the last expression in the block.
The else
branch is evaluated if none of the other branch conditions are satisfied.
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).
In when
statements, the else
branch is mandatory in the following conditions:
when
has a subject of aBoolean
,enum
, orsealed
type, or their nullable counterparts.branches of
when
don't cover all possible cases for this subject.
To define a common behavior for multiple cases, combine their conditions in a single line with a comma:
You can use arbitrary expressions (not only constants) as branch conditions
You can also check a value for being in
or !in
a range or a collection:
Another option is checking that a value is
or !is
of a particular type. Note that, due to smart casts, you can access the methods and properties of the type without any extra checks.
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:
You can capture when subject in a variable using following syntax:
The scope of variable introduced in when subject is restricted to the body of this when.
For loops
The for
loop iterates through anything that provides an iterator. This is equivalent to the foreach
loop in languages like C#. The syntax of for
is the following:
The body of for
can be a block.
As mentioned before, for
iterates through anything that provides an iterator. This means that it:
has a member or an extension function
iterator()
that returnsIterator<>
, which:has a member or an extension function
next()
has a member or an extension function
hasNext()
that returnsBoolean
.
All of these three functions need to be marked as operator
.
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 loops
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 ofdo-while
executes at least once regardless of the condition.
Break and continue in loops
Kotlin supports traditional break
and continue
operators in loops. See Returns and jumps.