Feature update – Using the keyword *for to iterate through the elements of a list


Going through the elements in a collection or association is now easier than ever! These examples will guide you in using the *for keyword:


Looping through the elements in a collection


>> farmAnimals = ["dog", "pig", "goat"]
*for: animal in farmAnimals
	There is a {animal} on the farm!

The code indented under the *for keyword will be executed as many times as elements there are in the collection farmAnimals (3 in the example). On each iteration, animal will be the element on the position of the current iteration. 

The output of the code above is:

 There is a dog on the farm!

 There is a pig on the farm!

 There is a goat on the farm!

 

Retrieving both the elements and their position inside a collection


In the previous example we generated a variable (animal) that, on each iteration, was set to the element in that position. But there may be cases where you want to store also the iteration number. Look at the example below:
>> highestPeaks = ["Everest", "K2", "Kangchenjunga"]

Here are the highest mountains on Earth:
*for: index, name in highestPeaks
	{index}. {name}

The *for loop will go through the collection highestPeaks, setting index to the iteration number and name to the element in that position. This is what the output looks like:

 Here are the highest mountains on Earth:

 1. Everest

 2. K2

 3. Kangchenjunga


In summary, when you are using *for to loop through the elements of a collection, you can either retrieve each of the elements in it using the syntax *for: element in collection or both the element and its position if you write *for: position, element in collection.

 

Looping through associations


In this example, my friend’s birthdays are stored in an association. The key of each element is my friend’s name, and the value is the date that they were born:
>> friendsBirthdays = {"Justin" -> "March 1, 1994", "Kim" -> "October 21, 1980", "Taylor" -> "December 13, 1989", "Beyonce" -> "September 4, 1981", "Michael" -> "August 29, 1958"}

*header: My friend's birthdays
*for: name, date in friendsBirthdays
	{name} was born on {date}

Alternatively, you may only want to get the value of each element, without the key. Here is how you can do that:
>> potluckSignup = {"Peter" -> "Deviled eggs", "Joan" -> "Hummus and carrots", "Kelly" -> "Cookies", "Andrew" -> "Pico de gallo"}

*header: Potluck dishes
*for: dish in potluckSignup
	- {dish}

 

Generate multiple pages using a *for loop


Suppose you have a program where the user has been asked to enter their favorite restaurants, which are stored in the collection favRestaurants. The code below will generate as many questions as restaurants they entered:
*for: restaurant in favRestaurant
	*question: What is your favorite food at {restaurant}? 

 

Loop through characters in a text variable


The *for keyword also allows you to go through all the characters in a string. For example, this is program displays a text variable called word letter by letter:
*for: character in word
	*clear
	*header: {character}
	*wait: 2.seconds

You could also retrieve the position of each character as well:
*question: Enter a word and I will spell it out for you!
	*save: word

*for: index, char in word
	*clear
	*header: The letter in position {index} is {char}
	*wait: 2.seconds