Posted 22 May 2015 - 07:38 PM
Conditional Statements and Loops
A Conditional statement is a situation where a computer program asks a question, and acts upon the results. If we were to write one in english, we would say if name is fisher, the person would say It's ok, guys! Its Fisher.
if name == "Fisher" then -- If the condition is true
print("It's ok, guys! Its Fisher :D/>/>") -- Do stuff!
end -- Um, nothing to see here :)/>/>
In this manner, we can also give an if statement a function or a variable, and if the function returns true, we can do something, or if a variable equals true, we can act upon it aswell.
Also, with If statements, we are able to use conditional operators, such as ( == - which is equals, ~= - which is not equals, and with math, we can use <, and >, which compare numbers, or the returned numbers from a function.
if "fisher" ~= "fosher" then -- This would be true
print("Fisher is not Fosher!") -- So, this code would run.
end
if 3 < 5 then -- This also works with > as seen above.
print("True") -- On these, you would also return false if it was a function for an api.
else -- :o/>/> what is this
print("False")
end
With these statements, we can also use else and elseif. Else is if the first, second, etc. statements didn't return true. So, we could say
if user == "fisher" then
print("Admin detected")
else
print("Intruder Alert!")
-- Here you would lockdown your base xP
end
The Same goes for elseif. If you have multiple conditions that could be met, like if you had multiple users to a password locked computer, you could do something like this.
if user == "fisher" then
print("User is fisher.")
elseif user == "dan200" then
print("Ermg... dan200 is on my server? lmao")
else
print("You aren't invited to the Party!")
end
You can also do a multitude of things with loops. Loops are repeating of code a certain number of times, or indefinitely. You just use the right kind of loop, and if you need to, specify how many times to loop, and you're off to the races!
While true do is the most common loop that people use in their programs. This will run, if I'm not mistaken, 20 times a second, which is MC's tps. So, if you don't pause it with a small sleep() or os.pullEvent(), it will throw an error something like
Too long without yielding.
Also, Why this works (if I'm not mistaken) is because true will always be true, because that's a set boolean. So, I haven't ever done this, but in theory, you can do while (condition is true or equals a variable) do, and it will do an action until you tell it not to.
-- While true do
while true do -- Will repeat until you break.
print("Wow, what a lovely day!")
sleep(.5) --Pauses the program for half a second.
end
-- The Code I proposed
while name == "fisher" do
print("He is still fisher")
sleep(.5)
end
-- Code that will eventually Error because of no yield in code.
while true do
nameOfFunctionHere()
end
And, last but not least, my favorite, the For loop. The for loop repeats certain code a certain amount of time. You specify the variable to count on, in my case and many other cases 'i', the number to start on, the number to end on, and (optionally) the number to count by. So, with this, we could write a piece of code like so.
for i = 1,5 do -- this will repeat the code five times, and start on 1
print("Fisher was here")
sleep(1)
end
-- every time the function is executed, the variable (in our case, i)
-- goes up the amount specified to count on. (Default 1.)
for i = 1,5 do
print(i..". Fisher") -- making good use of concatenation (putting together)
sleep(.2) -- sleeps .2 of a second.
end
--[[The output of this code will be
1. Fisher
2. Fisher
3. Fisher
4. Fisher
5. Fisher
]]--
This is a basic tutorial on Conditional Statements and Loops. Please Correct me if I am wrong on any of this (mistakes are probably hiding somewhere :P/> ). Sorry for my messy code and Posts xD. I hope this helped alot.
Happy Computing!
-XMedders