Reputation: 21
Why is the else
statement is not allowed to have a then
or other conditions?
Is it because it is the final condition within the else-if
conditions it represents?
I am struggling to understand this concept since I'm a beginner who just learned about variables.
I'm asking this because I received an error with my else
statement in the code:
message = 0
condition = 30
if condition <=10
message = “try harder”
elseif
condition <=20 then
message = "Almost learning"
else
condition = <=30 **—This is the line where I get the error message**
message = "Now you’re getting it"
end
print(message)
Would appreciate someone breaking down in laymen terms, why else is not allowed to have <
or >
or then
or other conditions.
Upvotes: 0
Views: 331
Reputation: 882606
else condition = <= 30
(which is the way your code was originally formatted) would be a very unusual feature in a language.
The whole point of else
on its own is to execute if none of the other conditions were true. So a condition on the else
is absolutely useless.
The Programming in Lua book if
statement shows the normal usage:
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
However, your actual code (when formatted correctly) ends up looking like:
else
condition = <=30
which is correct in terms of the else
but unfortunately makes the next line a statement. And this statement is very much incorrect syntax.
Now it may be that you meant to assign 30 to condition but, based on your other lines (that sort of match this as a condition), I suspect not. So it's probably best just to remove that line totally.
Upvotes: 2