While loops for iteration
A while loop can be used to iterate over a vector in a very similar way to a for
loop; the structure will just look a little different. Remember the stock_tickers
vector from the previous for
loop exercise? Let's revisit this same vector but using a while loop. Use a while
loop to iterate over the stock_tickers
vector, printing every element in that vector to the screen.
For the first example, you will need to hard-code the length of the vector when writing the conditions for your while
loop.
In the second part of this exercise, you will use length
to get the number of elements within the vector, removing the need to hard-code the vector length in conditions of the while
loop.
This exercise is part of the course
Intermediate Julia
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
ticker = 1
# Use a while loop to iterate over ticker
while ____ <= 4
# Print each ticker in stock_tickers
println(____[ticker])
# Increment ticker
____ = ____ + 1
end