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

What is the different of 'then if' and 'and' in an if statement?

Asked by 6 years ago

This is what i meant:

variable = 1
anothervariable = 2

if variable == 1 then
  if anothervariable == 2 then
--do stuff

What's the different with:

if variable == 1 and anothervariable == 2 then
--do stuff

2 answers

Log in to vote
2
Answered by 6 years ago

An if statment will create a new scope. Depending upon your code additional logic may be required in some cases.

Example:-

for i=1, 10 do
    if i % 2 == 0 then
        if i == 10 then
            -- this is a new scope
            print('This is the last number')
        end

        -- print all even numbers
        print(i)
    end
end

Combining the logic

for i=1, 10 do
    if i % 2 == 0 and i == 10 then
        print('This is the last number')
        print(i)
    end
end

As you can see the results of this code are not the same due to the different logic being executed.

"then if' and 'and' in an if statement" are not comparable as they do different tasks and change the logic of the code.

0
Yes! xAtom_ik 574 — 6y
0
Thank you! GamingOverlord756 48 — 6y
Ad
Log in to vote
-2
Answered by
Nep_Ryker 131
6 years ago

There's probably no difference, we just use the and statement to make our code look more efficient and it can probably reduce lag in your game.

Answer this question