It's easy to write simple loops like for (i in 0..10), but how to write more complex loops like:
for (byte i = 1 << 7; i != 0; i >>= 1)
or
for (byte i = 0x01; i != 0; i <<= 1)
Thanks for your help.
In case you don't like having a var and are ok with using a Sequence, you could also use something like generateSequence instead, e.g.:
generateSequence(1 shl 7) {
it shr 1
}
.takeWhile { it != 0 }
.forEach { ... }
// or: generateSequence(1 shl 7) { (it shr 1).takeIf { it != 0 } }.forEach { ... }
Otherwise Michaels answer about using while is perfectly fine.
vars and find myself using that style more and more (and sadly Java is quite bad at it). However, I guess for simple loops I will continue the old-fashioned way.for (seed; next-value-function; loop-termination-condition)-statements too often, so that I think I need this construct ;-)