Kotlin – Control Flow

Kotlin – Control Flow

Kotlin is a practical dialect henceforth like each useful dialect in Kotlin “if” is an articulation, it’s anything but a catchphrase. The articulation “if” will restore an esteem at whatever point fundamental. Like other programming dialect, “if-else” square is utilized as an underlying contingent checking administrator. In the accompanying precedent, we will analyze two factors and give the required yield appropriately.

fun main(args: Array<String>) { 
   val a:Int=5 
   val b:Int=2 
   var max: Int 
   if (a > b) { 
    max = a 
   } else { 
    max = b 
   } 
   print("Maximum of"+max) 
  
   // As expression  
  // val max = if (a > b) a else b 
}

The above bit of code yields the accompanying yield accordingly in the program. Our precedent additionally contains a different line of code, which portrays how to utilize “If” proclamation as an articulation.

When On the off chance that you know about other programming dialects, you may have known about the term switch articulation, which is fundamentally a restrictive administrator when numerous conditions can be connected on a specific variable. “at the point when” administrator coordinates the variable incentive against the branch conditions. On the off chance that it is fulfilling the branch condition, it will execute the announcement inside that scope. In the accompanying model, we will take in more about “when” in Kotlin.

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

The above bit of code yields the accompanying yield in the program.

x is neither 1 nor 2

In the above model, Kotlin compiler coordinates the estimation of x with the given branches. In the event that it isn’t coordinating any of the branches, it will execute the else part. Essentially, when is proportional to various if square. Kotlin gives another adaptability to the engineer, where the designer can give various checks in a similar line by giving “,” inside the checks. Give us a chance to adjust the above model as pursues.

fun main(args: Array<String>) { 
    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") 
    } 
} 
} 

Run the equivalent in the program, which will yield the accompanying yield in the program.

x is neither 1 nor 2

For Loop

Circle is such an innovation, to the point that gives the adaptability to repeat through any sort of information structure. Like other programming dialects, Kotlin likewise gives numerous sorts of Looping approach, in any case, among them “For” is the best one. The usage and utilization of For circle is thoughtfully like Java for circle. The accompanying precedent shows how we can utilize the equivalent, all things considered, models.

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

In the above bit of code, we have pronounced one rundown named as “things” and utilizing for circle we are emphasizing through that characterized rundown and printing its incentive in the program. Following is the yield.

values of the array 1

values of the array 2

values of the array 3

values of the array 4

Following is another case of code, where we are utilizing some library capacity to make our improvement work simpler than at any other time.

fun main(args: Array<String>) { 
    val items = listOf(1, 22, 83, 4) 
    for ((index, value) in items.withIndex()){ 
    println("the element at $index is $value") 
} 
} 

When we gather and execute the above bit of code in our coding ground, it will yield the accompanying yield in the program.

the element at 0 is 1


the element at 1 is 22


the element at 2 is 83


the element at 3 is 4

While Loop and Do-While Loop

While and Do-While work precisely correspondingly as they do in other programming dialects. The main distinction between these two circles is, if there should be an occurrence of Do-while circle the condition will be tried toward the finish of the circle. The accompanying model demonstrates the use of the While circle.

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

The above bit of code yields the accompanying yield in the program

Example of While Loop – Softwarelogy —

0

1

2

3

4

5

6

7

8

9

10

Kotlin additionally has another circle called Do-While circle, where the circle body will be executed once, at exactly that point the condition will be checked. The accompanying model demonstrates the utilization of the Do-while circle.

fun main(args: Array<String>) { 
   var x:Int=0 
   do{ 
       x=x+10 
       println("Hello Block > "+x) 
   }while(x<=50) 
     
}

The above bit of code yields the accompanying yield in the program. In the above code, Kotlin compiler will execute the DO square, at that point it will go for condition checking in while square

Hello Block > 10


Hello Block > 20


Hello Block > 30


Hello Block > 40


Hello Block > 50


Hello Block > 60

Return, Break, Continue

On the off chance that you know about any programming dialect, you should have a thought of various catchphrases that assistance us actualize great control stream in the application. Following are the diverse watchwords that can be utilized to control the circles or some other sorts of control stream.

Return: Return is a watchword that profits some an incentive to the calling capacity from the called capacity. In the accompanying precedent, we will execute this situation utilizing our Kotlin coding ground.

fun main(args: Array<String>) { 
   var x:Int=10 
   println("The value of X is="+doubleMe(x)) 
} 
fun doubleMe(x:Int):Int{ 
      return 2*x; 
}

In the above bit of code, we are calling another capacity and duplicating the contribution with 2, and restoring the resultant incentive to the considered capacity that is our principle work. Kotlin characterizes the capacity in an alternate way that we will take a gander at in a consequent part. For the present, it is sufficient to comprehend that the above code will produce the accompanying yield in the program.

The value of X is = 20

Continue and Break: Continue and break are the most crucial piece of a sensible issue. The “break” catchphrase ends the controller stream if some condition has fizzled and “proceed” does the inverse. This activity occurs with prompt perceivability. Kotlin is more intelligent than other programming dialects, wherein the engineer can apply more than one mark as perceivability. The accompanying bit of code indicates how we are actualizing this mark in Kotlin.

fun main(args: Array<String>) { 
    
   println("Example of Break and Continue") 
   [email protected] 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") 
           [email protected] //specifing the label 
       }else{ 
           println("I am inside else block with value"+x) 
           [email protected] 
       } 
   } 
   } 

The above bit of code yields the accompanying yield in the program.

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

As should be obvious, the controller proceeds with the circle, until and except if the estimation of x is 5. When the estimation of x achieves 5, it begins executing the if square and once the break articulation is achieved, the whole control stream ends the program execution.

Some examples are taken from the internet for clarity. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *