![]() ![]() While the first range will hold the list of multipliers corresponding to different rows, on the other hand, the second range will hold the result values. Further, we’ll need two ranges in our loop. * created by Chaitanya for Beginnersbook.Let’s advance our understanding of looping with multiple variables by applying concepts of ranges and the zip operator to generate multiplication tables, where each row shows up in the format: factor x multiplier = resultĪs the factor will remain the same in all the rows, we’ll have to loop through two multiplier and result variables. We can reverse a range in Kotlin using reversed() function. ![]() However if we want a specific interval like 3 then we can define the range like this 1.10.step(3) this way the values would be 1 4 7 10. With the for loop, you can also create ranges of values with '.'. By default the value of step is 1 so when we create range 1.10, it is 1, 2, 3.10. With the help of step() function we can define the interval between the values. we can use these functions rangeTo() and downTo(), rangeTo() is for increasing order and downTo() is for decreasing order. Println("3 in oneToTen: $")ġ1 in oneToTen: false Kotlin Range: rangeTo() and downTo() functions Lets see how to do this with the help of a simple example. We can also check whether a particular element is present in the range or not. We are cycling through the elements of the ranges using for loop. In the following example, we have created two ranges, one is an integer range and other one is a character range. We can also create ranges in reverse order and several other things with ranges. Similarly we can create character ranges such as ‘A’.’D’ which will create a series of values A, B, C, D. For example a range of 1.5 would create a series of values 1, 2, 3, 4, 5. ![]() With the help of ranges in Kotlin we can easily create a list of sequence by specifying starting and ending value. In this guide, we will discuss the very cool feature of Kotlin which is ranges. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |