Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Intermediate Julia

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

ticker = 1
# Use a while loop to iterate over ticker
while ____ <= 4
	# Print each ticker in stock_tickers
	println(____[ticker])
    # Increment ticker
    ____ = ____ + 1
end
Code bewerken en uitvoeren