Garry's Mod Wiki

Revision Difference

Beginner_Tutorial_If_Then_Else#517292

<cat>Dev.Lua</cat> # If Statements The "If" statement is a very common tool found in almost every programming language. This statement is a very basic, direct order to Lua. This can be really useful in creating your own scripts. "If" tells the code to do something if a condition is true or not. This is a simple if statement: ``` local age = 18 if age == 20 then print("You're 20!") end ``` The if statement can be a little confusing. The local age = 18 part of this script sets a "Local" variable named age as 18. Local just means that the variable will only exist within the control structure. In this case the if statement. The above example basically means "if age is equal to 20 then say 'You're 20!' and stop." The if statement looks like this to Lua: ``` if (Condition) then -- Do something end ``` Condition has to be true to run the code block after it. A code block is code that only runs under certain conditions. In this case, the code block runs if the condition is true. ## Else Statements Else is used to define the opposite of the condition, so if a condition is false, then else would tell it to execute the following code, a good usage of else is shown below. ``` local age = 40 if age == 20 then print("You're 20!\n") else print("You're NOT 20!\n") end ``` The above example checks if the variable "age" is 20, if this is true, then it prints to console "You're 20!", but if the variable "age" is NOT 20, then it prints "You're NOT 20!" to console. ## Conditions Available For Use ### Equal To The equal to condition is true if the thing on the left and right of == are equal. ``` local age = 1 if age == 20 then print("You are 20!\n") end ``` ### Not Equal To The "not equal to" condition is true if the thing on the left and right of ~= aren't equal. You can also use != instead of ~= ``` local age = 15 if age ~= 20 then print("You are NOT 20!\n") end ``` The code below is the same as the code above. ``` local age = 15 if age != 20 then print("You are NOT 20!\n") end ``` ### Less Than The "less than" condition is true if the thing on the left is less than the thing on the right. ``` local age = 15 if age < 20 then print("You are under 20!\n") end ``` ### Greater Than The "greater than" condition is true if the thing on the left is more than the thing on the right. ``` local age = 15 if age > 20 then print("You are over 20!\n") end ``` ### Less Than or Equal To The "less than or equal" condition is true if the thing on the left is less or equal to the thing on the right. ``` local age = 15 if age <= 20 then print("You are 20 or below!\n") end ``` ### Greater Than or Equal To The "greater than or equal" condition is true if the thing on the left is more or equal to the thing on the right. ``` local age = 15 if age >= 20 then print("You are 20 or over!\n") end ``` This is a lot for first time Lua scripters, however, if you practice enough with using Conditionals, you will get the hang of it eventually. # Else-If Statement Last, I want to show you how to do else if. Else if is used to say, basically, "If it's not that, then if..." in lua. This is more precise than using else, but else if is NOT effective all the time ``` local age = 15 if age < 15 then print("Sorry, you need to be at least 15.\n") elseif age > 18 then print("Sorry, nobody over 18 is allowed in here.\n") else print("Welcome to High School!") end ``` What this script is doing is trying to figure out if you can go to high school. If you're not under 15, and you're not over 18, then you can come in. Give it a try yourself. Try writing a simple if statement in notepad. If you need help just look at the examples in here. Start with the simple ones and work up. ⤶ ⤶ ⤶ # What's next?⤶ ⤶ Next we will learn about [operators](/gmod/Beginner_Tutorial_Tables).⤶