How to make a multiconditional loop in Kotlin
Conditional loops are common to any programming language you pick. If you apply multiple conditions on a loop, it is called a multiconditional loop. A simple example of a multiconditional loop in Java is illustrated here:
int[] data = {5,6,7,1,3,4,5,7,12,13}; for(int i=0;i<10&&i<data[i];i++){ System.out.println(data[i]); }
The preceding code on execution will print out 5
, 6
, and 7
. Let's see how we can use a multiconditional loop in Kotlin. We will be looking at a functional approach to the same thing in Kotlin.
Getting ready
You need to install a preferred development environment that compiles and runs Kotlin. You can also use the command line for this purpose, for which you need Kotlin compiler installed, along with JDK. I am using IntelliJ IDE to compile and run my Kotlin code for this recipe.
How to do it...
The preceding multiconditional loop can be written in Kotlin like so:
(0..9).asSequence().takeWhile { it<numbers...