Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

Yes!

0

Thank you!

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.