You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Will for... else work with both For and For In expression?
Something like these
for i in 0..-1 {
echo("inside for")
} else {
echo("inside else")
}
for i = 0; i < 10; i++ {
if i == 11 { echo ("inside for") }
} else {
echo("inside else")
}
I think for in is enough. The basic idea is to run some code if list in the for in is empty
users = db.query("SELECT students WHERE age > 20")
for user in users {
print(user)
} else {
print("We don't have students above the age of 20")
}
which you would have to write like this without for else:
users = db.query("SELECT students WHERE age > 20")
if users.len() {
for user in users {
print(user)
}
} else {
print("We don't have students above the age of 20")
}
or
users = db.query("SELECT students WHERE age > 20")
if users.len() {
for user in users {
print(user)
}
return
}
print("We don't have students above the age of 20")
BTW python does this differently (the else runs when there's no break), but I believe it'd be more useful to keep for ... else for other use cases. Jinja does this (even though it's a templating language):
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users found</em></li>
{% endfor %}
</ul>
No description provided.
The text was updated successfully, but these errors were encountered: