Kotlin - Control Flow
If
- Else
Kotlin is a functional language
hence like every functional language in Kotlin “ if”
is an expression, it is not a keyword. The expression “if”
will return a value whenever necessary . Like other programming
language, “if-else”
block is used as an initial conditional checking operator. In the
following example, we will compare two variables and provide the
required output accordingly.
The above piece of code yields the
following output as a result in the browser. Our example also
contains another line of code, which depicts how to use “If”
statement as an expression.
Use
of When
If you are familiar with other
programming languages, then you might have heard of the term switch
statement, which is basically a conditional operator when multiple
conditions can be applied on a particular variable. “when”
operator matches the variable value against the branch conditions. If
it is satisfying the branch condition then it will execute the
statement inside that scope. In the following example, we will learn
more about “when” in Kotlin.
In the above example, Kotlin
compiler matches the value of x
with the given branches. If it is not matching any of the branches,
then it will execute the else part. Practically, when is equivalent
to multiple if block. Kotlin provides another flexibility to the
developer, where the developer can provide multiple checks in the
same line by providing “,”
inside the checks. Let us modify the above example as follows.
For
Loop
Loop is such an invention that
provides the flexibility to iterate through any kind of data
structure. Like other programming languages, Kotlin also provides
many kinds of Looping methodology, however, among them “For”
is the most successful one. The implementat ion and use of For loop
is conceptually similar to Java for loop. The following example shows
how we can use the same in real-life examples.
In the above piece of code, we have
declared one list named as “items” and using for loop we
are iterating through that defined list and printing its value in the
browser. Following is the output.
Following is another example of
code, where we are using some library function to make our
development work easier than ever before.
While
Loop and Do-While Loop
While and Do-While work exactly in a
similar way like they do in other programming languages. The only
difference between these two loops is, in case of Do-while loop the
condition will be tested at the end of the loop. The following
example shows the usage of the While
loop.
Kotlin also has another loop called
Do-While loop, where the loop body will be executed once, only then
the condition will be checked. The following example shows the usage
of the Do-while loop.
Use
of Return, Break, Continue
If you are familiar with any
programming language, then you must have an idea of different
keywords that help us implement good control flow in the application.
Following are the different keywords that can be used to control the
loops or any other types of control flow.
Return:
Return is a keyword that returns some value to the calling function
from the called
function. In the following example, we will implement this scenario
using our Kotlin coding ground.
In the above piece of code, we are
calling another function and multiplying the input with 2, and
returning the resultant value to the called function that is our main
function. Kotlin defines the function in a different manner that we
will look at in a subsequent chapter. For now, it is enough to
understand that the above code will generate the following output in
the browser.
Continue
& Break:
Continue and break are the most vital part of a logical problem. The
“break”
keyword terminates the controller flow if some condition has failed
and “continue”
does the opposite. All this
operation happens with immediate visibility. Kotlin is smarter than
other programming languages, wherein the developer can apply more
than one label as visibility. The following piece of code shows how
we are implementing this label in Kotlin.
Comments
Post a Comment