Introduction = 10 println(“found–“+i) } } Output: 123456789 found–2


Kotlin is a programming language
introduced by JetBrains.It is strongly statically typed language.It runs on
JVM. Google announced Kotlin is an official language for android development in
2017. It is an open source programming language that combines object-oriented
programming and functional features into a unique platform.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now


Data Types

Data types available are:



Following are
variable length datatype of numbers:














fun main(args: Array) {   val a: Int = 200   val d: Double = 133.00   val f: Float = 120.00f   val l: Long = 1004564012   val s: Short = 5   val b: Byte =     }



Kotlin character is represented using char. A character is declared
in a single quote like ‘a’. Variable are declared in two ways :”var” and
another “val”.

fun main(args: Array)

   val letter: Char    // defining a variable

   letter = ‘A’        // Assigning a value to it



Strings are character
arrays. They are immutable. There are two types of string: raw String and
escaped String.



Arrays are a
collection of same type of data. It supports arrays of different data types.

fun main(args: Array)

   val numArray: IntArray = intArrayOf(12, 23, 34,
42, 51)






Collection is a part
of the data structure. There are two types of collection: immutable
collection (lists, maps and sets) & mutable collection (type
of it is editable).


Ranges is unique
characteristic of Kotlin. It provides an operator that helps you iterate
through a range. It is implemented using rangeTo() .It uses
operator form as (..).

fun main(args: Array)

   val i:Int 
= 4

   for (j in 1..9)

   print(j) // prints “123456789”


   if (i in 1..10) { // equivalent of 1 < = i && i < = 10       println("found--"+i)    } } Output: 123456789 found--2         Control flow If - Else The expression "if" returns a value whenever necessary. Like other programming language, "if-else" block is used as an initial conditional checking operator. fun main(args: Array)

   val a1:Int = 12

   val b1:Int = 24

   var maximum: Int

   if (a1 > b1) {

      maximum = a

   } else {

      maximum = b1


   print(“Maximum of a1 or b1 is ” + maximum) 



Maximum of a1
or b1 is 5



when operator matches the
variable’s value against the branch conditions. If it satisfies the branch condition,
then it executes the statement inside that scope.

fun main(args: Array)

   val x:Int = 5

   when (x) {

      1 -> print(“x = = 1”)

      2 -> print(“x = = 2”)


      else -> {  

         print(“x is neither 1 nor 2”)





x is neither 1
nor 2

Compiler matches the
value of x with the given branches. If it does not match any of
the branches, then it will execute the else part.


Loop can iterate
through any kind of data structure. The implementation and use of For loop is similar
to Java for loop.


fun main(args: Array)

   val items = listOf(13, 22, 34, 44)

   for (i in items) println(“values of the



values of the

values of the

values of the

values of the


loop and Do-While loop

While and Do-While
works same as in other programming languages. In case of Do-while loop the
condition will be tested at the end of the loop. In case of while loop
condition will be tested at the start of the loop.

fun main(args: Array)

   var x:Int = 0

   println(“Example of While Loop–“)

   while(x< =5) {       println(x)       x++    } } The above piece of code yields the following output in the browser. Example of While Loop-- 0 1 2 3 4 5     fun main(args: Array)

   var x:Int = 0

   do {

      x = x + 10

      println(“I am inside Do

   } while(x <= 40) } Compiler will execute the DO block, then it will go for condition checking in while block.     I am inside Do block---10 I am inside Do block---20 I am inside Do block---30 I am inside Do block---40   Return,Break,Continue 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.It returns value to the calling function from the called function. fun main(args: Array)

   var x:Int = 1

   println(“The value of X is–“+ myfun(x))


fun myfun(x:Int):Int

   return 2+x;



The value of X


Continue & Break: The “break” keyword
terminates the controller flow if some condition has failed and “continue” does
the opposite.

fun main(args: Array)

   [email protected] for(x in 1..10) { // appling the
custom label

      if(x = = 6) {

         println(“I am inside if block
with value”+x+”
— hence it will close the operation”)

         [email protected] //specifing the label

      } else {

         println(“I am inside else block
with value”+x)

         [email protected]





Example of Break
and Continue

I am inside
else block with value1

I am inside
else block with value2

I am inside
else block with value3

I am inside
else block with value4

I am inside
else block with value5

I am inside if
block with value6


The controller
continues the loop, until and unless the value of x is 6. Once
the value of x reaches 6, it starts executing the if block and
once the break statement is reached, the entire control flow terminates the
program execution.


A class is a
blueprint of an entity and object is its state. It includes both behavior and
state. Class declaration consists of two components: class header & class

Class myclass {
// class Header

   // class Body


class myclass {

   // data member

   private var name: String = “Infosys”


   // member function

   fun printMessage() {

      print(“You are working in best
company named-“+name)



fun main(args: Array)

   val myobject = myclass() // create obj
object of myClass class




You are working
in best company named:Infosys