WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Control Flow

kotline have different types of control flow mechanism


If - Else

“if “ will return a value whenever necessary.But “if-else “ block is used as an initial conditional checking operator.
fun main(args: Array<String>) {
    val x:Int = 7
    val y:Int = 5
    var max: Int

    if (x > y) {
        max = x
    } else {
        max = y
    }
    print("Maximum of x or y is " +max)

    // As expression
    // val max = if (x > y) x else y
}
output
Maximum of a or b is 7

When

“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

fun main(args: Array<String>) {
   val x:Int = 5
   when (x) {
      1 -> print("x = = 2")
      2 -> print("x = = 3")
      
      else -> { // Note the block
         print("x is neither 2 nor 3")
      }
   }
}

Output
x is neither 2 nor 3
In Kotlin we can change same line by providing “,” inside the checks.Let us modify the above example as follows.
fun main(args: Array) {
   val x:Int = 5
   when (x) {
      1,2 -> print(" Value of X either 1,2")
      
      else -> { // Note the block
         print("x is neither 1 nor 2")
      }
   }
}


Output
x is neither 2 nor 3

For Loop

Loop is similar to just like java.

fun main(args: Array) {
   val items = listOf(1, 2, 3, 4)
   for (i in items) println("values of the array"+i)
}
output

using for loop we are iterating through that defined list and printing its value in the browser.


values of the array1
values of the array2
values of the array3
values of the array4

we are using some library function to make our development work easier than ever before.

fun main(args: Array) {
   val items = listOf(1,12,53, 3)
   
   for ((index, value) in items.withIndex()) {
      println("the element at $index is $value")
   }
}

it will yield the following output in the browser.

the element at 0 is 1
the element at 1 is 12
the element at 2 is 53
the element at 3 is 3

While Loop and Do-While Loop

fun main(args: Array) {
   var x:Int = 0
   println("Example of While Loop--")
   
   while(x< = 8) {
      println(x)
      x++
   } 
}


following output in the browser.

Example of While Loop--
0
1
2
3
4
5
6
7
8

Kotlin also has another loop called Do-While loop

The following example shows the usage of the Do-while loop.

fun main(args: Array) {
   var x:Int = 0
   do {
      x = x + 10
      println("I am inside Do block---"+x)
   } while(x <= 30)
}

Kotlin 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
 

Use of Return, Break, Continue

following are the different keywords that can be used to control the loops

Return :

Return is a keyword that returns some value to the calling function from the called function Example
fun main(args: Array) {
   var x:Int = 20
   println("The value of X is--"+doubleMe(x))
}
fun doubleMe(x:Int):Int {
   return 2*x;
}
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. output
The value of X is--40

Continue & Break

Continue and break are the most vital part of a logical problem. The following piece of code shows how we are implementing this label in Kotlin
fun main(args: Array<String>) {
   println("Example of Break and Continue")
   myLabel@ for(x in 1..10) { // appling the custom label
      if(x = = 5) {
         println("I am inside if block with value"+x+"\n-- hence it will close the operation")
         break@myLabel //specifing the label
      } else {
         println("I am inside else block with value"+x)
         continue@myLabel
      }
   }
}
The above piece of code yields the following output in the browser.
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 if block with value5
-- hence it will close the operation

As you can see, the controller continues the loop, until and unless the value of x is 5. Once the value of x reaches 5