for (var i = 1; i < 1024; i *= 2) {
print(i)
}
How can this be done with for in loop?
The given solution is for +=
operator not *=
operator. Please provide a solution for *=
thanks.
for (var i = 1; i < 1024; i *= 2) {
print(i)
}
How can this be done with for in loop?
The given solution is for +=
operator not *=
operator. Please provide a solution for *=
thanks.
In Swift 3 you can do
The concept of the
sequence
function is described in the documentation.Printing an infinite list is easy, the code would just be
Since we want the program to stop at some point, we us the ternary operator
?
to terminate the list once we reach the maximum value. Since the last value we want to print is512
, the last value we have to double is256
. For512
which does not satisfy the condition< (1024 / 2)
we havenil
and thereby stop.