WEB Button DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Kotlin - Control Flow

In the previous chapter we have learned about different types of data types available in Kotlin system.


If - Else

The expression “if” will return a value whenever necessary.“if-else” block is used as an initial conditional checking operator. In the following example, we will compare two variables.
 

fun main(args: Array<String>) { 

val x:Int = 7 

val y:Int = 5 

var max: Int 

on the off chance that (x > y) { 

max = x 

} else { 

max = y 

} 

print("Maximum of x or y is " +max) 

/As articulation 

/val max = if (x > y) x else y 

} 

Our example also contains another line of code, which depicts how to use “If” statement as an expression. output
 

Limit of an or b is 7 


When

Our example also contains another line of code, which depicts how to use “If” statement as an expression. If it is satisfying the branch condition then it will execute the statement inside that scope.

In the accompanying instance

 

fun main(args: Array<String>) { 

val x:Int = 5 

whenever (x) { 

1 - > print("x = 2") 

2 - > print("x = 3") 

else - > {/Note the square 

print("x is neither 2 nor 3") 

} 

} 

} 

Output
 

x is neither 2 nor 3 

In Kotlin we will exchange meet line by utilizing providing “,” in the checks.Let us adjust the above precedent as follows.

 

fun main(args: Array<String>) { 

val x:Int = 5 

whenever (x) { 

1,2 - > print(" Value of X either 1,2") 

else - > {/Note the square 

print("x is neither 1 nor 2") 

} 

} 

} 

Output
 

x is neither 2 nor 3 

For Loop

Loop is like simply like java.

Kotlin also provides many kinds of Looping methodology, however, among them “For” is the most successful one.implementation and use of For loop is conceptually similar to Java for loop.
 

fun main(args: Array<String>) { 

val things = listOf(1, 2, 3, 4) 

for (I in things) println("values of the array"+i) 

} 

output

using for loop we are emphasizing through that characterized rundown and printing its incentive in the browser.

 

estimations of the array1 

estimations of the array2 

estimations of the array3 

estimations of the array4 

we are utilizing some library capacity to make our advancement work less demanding than any time in recent memory before.

 

fun main(args: Array<String>) { 

val things = listOf(1,12,53, 3) 

for ((list, esteem) in items.withIndex()) { 

println("the component at $index is $value") 

} 

} 

it will yield the accompanying yield in the browser.

 

the component at 0 is 1 

the component at 1 is 12 

the component at 2 is 53 

the component at 3 will be 3 

While Loop and Do-While Loop

Once we compile and execute the above piece of code in our coding ground, it will yield the following output in the browser. While and Do-While work exactly in a similar way like they do in other programming languages.
 

fun main(args: Array<String>) { 

var x:Int = 0 

println("Example of While Loop- - ") 

while(x< = 8) { 

println(x) 

x++ 

} 

} 

following yield in the browser.

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

Kotlin also has some other loop known as Do-While loop

The following precedent shows utilizing the Do in the meantime as loop.

 

fun main(args: Array<String>) { 

var x:Int = 0 

do { 

x = x + 10 

println("I am inside Do square - "+x) 

} while(x <= 30) 

} 

Kotlin compiler will execute the DO square, at that point it will go for condition checking in while block.

 

I am inside Do square - 10 

I am inside Do square - 20 

I am inside Do square - 30 

Use of Return, Break, Continue

following are the distinctive catchphrases that can be utilized to control the loops

Return :

Return is a watchword that profits some an incentive to the calling capacity from the called capacity Example
 

fun main(args: Array<String>) { 

var x:Int = 20 

println("The estimation of X is- - "+doubleMe(x)) 

} 

fun doubleMe(x:Int):Int { 

return 2*x; 

} 

In the above bit of code, we are calling each other component and duplicating the enter with 2, and restoring the following expense to the known as capacity this is our significant capacity. output
 

The estimation of X is- - 40 

Continue and Break

Proceed and break are the most crucial piece of a legitimate issue. The accompanying bit of code demonstrates how we are executing this name in Kotlin
fun main(args: Array<String>) { 

println("Example of Break and Continue") 

myLabel@ for(x in 1..10) {/appling the custom name 

if(x = 5) { 

println("I am inside if obstruct with value"+x+"\n- - thus it will close the task") 

break@myLabel/specifing the mark 

} else { 

println("I am inside else hinder with value"+x) 

continue@myLabel 

} 

} 

} 

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

Case of Break and Continue 

I am inside else obstruct with value1 

I am inside else obstruct with value2 

I am inside else hinder with value3 

I am inside else obstruct with value4 

I am inside if hinder with value5 

- consequently it will close the task 

As you may see, the controller keeps up the loop, until and with the exception of the expense of x is 5. When the expense of x achieves five