Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does the 'if' statement work similar to 'PEMDAS' (math/parentheses)? [closed]

Asked by 9 years ago

What do I mean? I mean, does the if statement work with parentheses just like with Math Equations? As for example, if we did a math equation such as (a - b) * c.

How do you set up the code as such? I set up the code as so;

if (a or b or c or d) and e then
    --Code Chunk
end

What are you unsure of exactly? I am unsure if it will check (basing this off the last code chunk,) a, b, c, or d, before heading on to e for a condition. I have been unsure of this for a week now, and I can not figure out if it will work as I have explained.

0
if a or b or c or d and e. This works TheDeadlyPanther 2460 — 9y

Locked by TheeDeathCaster

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
Necrorave 560 Moderation Voter
9 years ago

I believe the answer is a simple Yes, it is similar

Using your example

Anything inside the parenthesis will be considered one condition. You are clumping these conditions together. A simple comparison would be this (2+2) = (4) Same goes for bools. (true and false) = false while (false or true) = true

Lets say we have the following:

if (a and b) or c then
--Code
end

What this will do is check the first part of the conditional statement a and b. As you may already know, it will check to see if a and b are both true.

Although, because they are placed inside of parenthesis, they are considered a single condition.

If that turns out to be true, it will not bother checking the next condition because it has already succeeded regarding the or

Another example:

if (a and b) or (c or d) then
--Code
end

This gets a bit more tricky. It will check if a and b are both true. Either way, it will continue to the next condition because of the or

It would then check if c or d are true. If either of them are, the code will run through the statement as if true, regardless of what a and b resulted in.

A more numerical example:

if ((math.random(10) - 5) + 1 < (math.random(10) - 5) + 1) then
--Code
end

This will first follow PEMDAS and subtract 5 from the random number generated. Then it will add 1 to it afterwards. Which it would then compare to the same condition after the <and repeat the same process.

Does that answer any questions you had? If not, please let me know.

EDIT: To answer your comment, Yes. It will check things outside the parenthesis as long as the rest of the conditional allows it.

(a or b) and c will still check if c is true too because the statement relies on both conditions being true. Hence the and

Parenthesis are just a way to manipulate the data within the statements, but it will always go check the rest (Unless its an or of course)

PS: I know this is a rather hard topic to explain, was this ideal?

0
So if I were to have it like in the code chunk example I gave, it will first check the variable(s) within the parentheses, then the one(s) outside? Or it will just check the one(s) inside the parentheses (if true,) and not check the outside variable(s)? Just making sure. :P TheeDeathCaster 2368 — 9y
0
It depends on the rest of the conditional, I will add an edit at the bottom explaining that particular question. Necrorave 560 — 9y
Ad