Pages

Sunday, June 3, 2018

Learn Kotlin 6 (Control flow 2:Hi friends this is the continuation of previous post so i kindly request you to read that before this post. As this blog is also prepared for beginners so i advice you that you don't need to by-heart this lessons just read and understand it if you have any doubt ask in the comment section )

for Loops

for loop iterates through anything that provides an iterator and here is an example then we can explain about the for loop :

for(items in collection) print (item)

here the for loop arguments are a "collection" and "items" the elements in the collection is put on items one by one and printed out in the print statement inside the for loop until collection is over. the body can be a block also like given below as:

for (items:Int in ints){
//the code to be executed here
}
the for loop has a member or extension function which is iterator() whose return type has a next() and hasnext() function which returns boolean all of this three function have to be marked as operators to iterate over a range you can use range expression for example:

for (i in 1..5){
 println(i)
}
for (i in 6 downTo 0 step 2){
println(i)
}
//the output will be as
1
2
3
4
5
6
4
2
0
the first one were i is printed out the value from range 1 to 5 and in the second for loop i is assigned value from 6 to 0 going down so the "downTo" keyword is used and printed out from 6 to 0 with a step of 2 so it out puts 6,4,2,0 with a difference of 2
A for loop over a range or an array is compiled to an index based loop that does not create an iterator object here is
var array = arrayOf("a","b","c")
for(i in array.indices){
println(array[i])
}
//the output will be as
a
b
c

In here we can see that the array indices are put on the i and printed out as array[i] now there is other one that is
var array = arrayOf("a","b","c")
for ((index,value) in array.withIndex()){
println("the value at $index is $value")
}
//it will print out as
the value at 0 is a
the value at 1 is b
the value at 2 is c
In here the index of the array is put on the index argument and value is put on the value argument and printed out .

while and do while loop

while loops and do while loop works same as  in java language
var x = 6
while(x < 2 ){
print(x)
 x--
}
var y = 2
do {
y++
}while(y<10)

In here the first while loop will execute the statement x-- means it decreases the value of x by one each time until the condition in the while loop become true and the next is the do while loop where the statement in do will execute until the condition in the while loop becomes true and the difference between while and do while loop is that in do while loop if the condition became true then also it will execute one time before the condition is checked so the result of while loop above will be as
6,4,3 and the result of do while loop will be 3,4,5,6,7,8,9,10 here the the condition is less than 10 but it is executed till 10 that is the difference

 Return statements

The above three are the kotlins structural jump expressions return statement terminates the execution of a function and returns control to the calling function and also can be used to return the value which comes as a result of the function to the calling function by default returns from the nearest enclosing function or anonymous function we can now see an example
var x = 32
var y = 34
fun add (x:Int,y:Int):Int{
 return x+y;
}
print(add(x,y))
//it will output the value as 66

here we have a function called add and it will return the sum of x and y and is printed using the print statement


Thats all for this post guys hope you like the post to get the latest posts on this follow by email and follow me don't forget to share and comment below your doubts and suggestions Thankyou all  

No comments:

Post a Comment