Log In  

Hi guys:
This is kinda embarrassing but I'm not 100% sure how ELSE works.

If an if condition is originally TRUE, does the ELSE say "Well I'll do this is it's FALSE"?

c="ORANGE"
IF c="ORANGE" then print "YES" (This is definitely true)
ELSE print "NO" (Somewhere in the code, c changes to APPLE)


But what about OR comparisons?

c="ORANGE"
IF c="PLUM" OR c="GRAPE" then then print "YES"
ELSE print "NO" (It does this line because c is NEITHER PLUM or GRAPE)


I think that's correct so far. But I get a bit stuck on ELSEIF

c="ORANGE" b="APPLE"
IF c="PLUM" OR c="GRAPE" then then print "YES"
ELSEIF b="APPLE" print "THIS LINE!"

Does the psudo code above confirm the fact that c is NOT PLUM or GRAPE but sees that b is APPLE so runs that and prints "THIS LINE!"

It's hard to explain why I get stuck. I hope someone gets what I'm trying to ask.

Cheers

P#98603 2021-10-13 22:07

First, be careful in your comparisons: always 'if c=="orange"', never '=' which assigns a value and does not compare.

For your question: yes, else runs if the expression evaluated by if is not true. It doesn't matter if you have 'if something' (goes to 'else' only if something is false or nil), 'if something < 20' (goes to 'else' if something is 20 or higher), 'if not something.dead and something.blip and something.blip == "bloop"' (goes to else if something.dead is true / if dead is false but something.blip is false or nil or not defined / if dead is false and blip is not equal to "bloop"). To put it differently, 'if' and 'else' don’t know what’s in the expression; the format is really 'if anything-here-that-is-checked-to-be-not-false-or-nil then do stuff else do other stuff'.

'elseif' is really the same as having another 'if' inside an 'else' block, but one line more compact.

P#98608 2021-10-13 22:43 ( Edited 2021-10-13 22:44)

The syntax for if statements are:

IF [expression] THEN [statements] ELSE [statements] END

The expression is a boolean check which evaluates to true or false. i.e. (x==10) or (y>20) or even (z=="Hello."). When this check passes, the statement(s) after THEN will be executed, but not the ones after ELSE. If the check is false, the statements after ELSE are executed instead of the ones after THEN.

You can omit the ELSE keyword if you don't have any code to run when the check evaluates to false. i.e.

IF x==10 THEN print(x) END

If you want to check things the other way around, like needing to know if something is NOT equal or NOT less than something, the format becomes IF NOT [exp] THEN [stmt] END. You can of course just implement the negated behavior in the expression itself. Things like (x!=10), or (y<=20) instead of (y>20).

Logically IF / THEN / ELSE statements break down into simple boolean logic:

IF (x==10) THEN print(x) ELSE print("none") END

becomes

(x==10) AND print(x) OR print("none")

Most statements which are built in, such as print(t), will return true. You can check this in PICO-8 in interpreter mode by typing in print(NOT NOT print("t")) (this returns the string "t" and then "true" on the next line.

We can make a handy function for checking logic as follows:

function bool(x) return not not x end
?bool(print("x")) -- to check the functionality
x -- output
true -- output

The print(x) command actually returns the number of bytes of the string or item given. Since this is always a number, and numbers evaluate to true, you can simply assume print(x) evaluates to true by association.
In the example above, if x is indeed 10, then the logic becomes:

(T) AND (2) OR (3)

Logic is evaluated from left to right. The expression can terminate early if it has a true value.

(T) AND (2) -- 1 and 2 are both true, evals to true or (T)
(T) OR (3) -- (T) is satisfied. The OR check does not change the state and is not evaluated.

What if X was not 10, but not equal to 10? The logic becomes:

(F) AND (2) OR (3)

We know that (F AND 2) will evaluate to false. (2) is not evaluated because the AND check will not change the evaluation of false. However when we check (F OR 3), (3) is evaluated because it is true, satisfying the OR check to be true and allowing evaluation of the expression of (3).

Also, the first expression is always evaluated. This begins the chain reaction of processing the other checks in the entire expression. If the first statement becomes true, the following happens:

TRUE -> all subsequent ANDs must be true to evaluate.
TRUE -> all subsequent OR checks are ignored.

FALSE -> any true statement proceeding OR can change the result to true.
         in this case, revert back to the rules about TRUE
FALSE -> all AND checks are ignored / does not change return state

I hope this helps!

P#98609 2021-10-13 23:47 ( Edited 2021-10-13 23:53)
c="ORANGE" b="APPLE"
IF c="PLUM" OR c="GRAPE" then then print "YES"
ELSEIF b="APPLE" print "THIS LINE!"

lets write it a little bit diffrent:

c="orange"
b="apple"
if c=="plum" or c=="grape" then
  print("yes")
elseif b=="apple" then
  print ("this line!")
end

is the same as

c="orange"
b="apple"
if c=="plum" or c=="grape" then
  print("yes")
else
  if b=="apple" then
    print ("this line!")
  end
end

elseif is only a shortcut - and always indent lines, it makes so many things easier.

so your code first check if c is "plum" or "grape". When yes it print "yes" and jump over the end of this snippet.
When c is not "plum" and not "grape" the code in the else-part is executed. In this case you have a nested second if statement. And when then b is "apple", it output "this line!".

but what happend when you:

c="plum"
b="apple"
if c=="plum" or c=="grape" then
  print("yes")
else
  if b=="apple" then
    print ("this line!")
  end
end

in this case c is "plum", so the first if-statment ist true and it will print "yes".
the nested if in the else part will never be executed here!
You can short this to:

c="plum"
b="apple"
if c=="plum" or c=="grape" then
  print("yes")
elseif b=="apple" then
  print ("this line!")
end

Same as above - it prints only "yes"!

And remember "or" means here "at least one of the conditions must be true". for example

c="circle"
b="box"
if c=="circle" or b=="box" then
  print("yes")
else
  print ("no!")
end

will output "yes"

P#98623 2021-10-14 07:48 ( Edited 2021-10-14 21:27)

you got some = mixed with == in that last message!

P#98633 2021-10-14 14:08

Many thanks for the help here merwok, GPI & MrGoober. Much appreciated.
Cheers

P#98644 2021-10-14 19:08

@merwok
ups. I didn't test the code :) - my problem is, that I primär program in Basic and there are no ==
funny thing is, that lua doesn't allow an assign of a value in the If, while ... statments und could do the same.

P#98650 2021-10-14 21:29

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 19:28:24 | 0.031s | Q:19