Loops
🔂 Looping a set number of times or over a range
🔂 name range 🍇 … 🍉 repeats the block, putting each value in the range into the name in turn. You create a range with 🆕⏩ start end❗️. The end value is not included.
🏁 🍇
🔂 i 🆕⏩ 0 5❗️ 🍇
😀 🔡 i❗️❗️
🍉
🍉0 1 2 3 4
If you pass a third value, the loop advances by that step. You can also count down with a negative step.
🏁 🍇
🔂 i 🆕⏩ 0 10 3❗️ 🍇
👄 🔤🔡 i❗️ 🔤❗️
🍉
😀 🔤🔤❗️
🔂 i 🆕⏩ 3 0 -1❗️ 🍇
👄 🔤🔡 i❗️ 🔤❗️
🍉
😀 🔤🔤❗️
🍉0 3 6 9 3 2 1
🔂 Going through a list
You can also pass a list (Lists and Dictionaries) to 🔂.
🏁 🍇
🍿 🔤apples🔤 🔤oranges🔤 🔤grapes🔤 🍆 ➡️ fruits
🔂 fruit fruits 🍇
😀 🔤I like fruit🔤❗️
🍉
🍉I like apples I like oranges I like grapes
🔁 While a condition holds
🔁 condition 🍇 … 🍉 repeats the block as long as the condition is 👍.
🏁 🍇
1 ➡️ 🖍🆕 n
🔁 n ◀️ 100 🍇
n ⬅️✖️ 2
🍉
😀 🔡 n❗️❗️
🍉128
Leaving a loop early
Emojicode has nothing like break, which leaves a loop early, or continue, which skips to the next iteration. Either put the exit condition into the loop's condition, or move the loop into a function (method) and return with ↩️.
🏁 🍇
💭 Find the first multiple of 7 greater than 50
1 ➡️ 🖍🆕 n
👎 ➡️ 🖍🆕 found
🔁 ❎ found❗️ 🍇
n ⬅️➕ 1
↪️ n 🚮 7 🙌 0 🤝 n ▶️ 50 🍇
👍 ➡️ 🖍found
🍉
🍉
😀 🔡 n❗️❗️
🍉56