Advertisement
XxSpiritDuoxX

"if then" Statements

Feb 10th, 2022
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Here is a something you will definitely use alot: "if then" statements
  3.  
  4. "if then" statements are script lines with conditions, if the conditions pass, it does X
  5.  
  6. if not, it will either do nothing or do Y
  7.  
  8. Here is a basic example of one:
  9. ]]
  10.  
  11. if 3 > 2 then
  12.     print("Complete!")
  13. end
  14. --[[
  15. As you can see, this is an example of an "if then" statement, What this does is check if 2 is
  16. higher than 3
  17.  
  18. You will notice how the above uses a "greater than" symbol, These are a list of operators that
  19. an 'if then' statement will use:
  20.  
  21. >  | Greater than
  22. <  | Lesser than
  23. == | Equal to
  24. ~= | not equal to
  25. >= | Greater than or is equal to
  26. <= | Lesser than or is equal to
  27.  
  28. These operators are commonly used for integer or float values, But, You can use them for object values
  29. or other values as well (mainly == and ~=)
  30.  
  31. There are also another thing called "elseif"
  32.  
  33. This is a secondary conditonal if the first one fails
  34.  
  35. Meaning that if the first conditional fails, it checks if the second conditional true, and then does
  36. something else
  37.  
  38. Anyways, here is a somewhat more 'complex' example of an "if then" statement
  39. ]]
  40.  
  41. if 125 > 500 then --Checks if 125 is higher than 500
  42.     print("Success") --If yes, then print "Success"
  43. elseif 325 < 200 then --If the conditions above isnt met, then check if this conditions work
  44.     print("Success 2") --If the above succeeds, then print "Success 2"
  45. else --If all the conditions above fail, do this instead
  46.     print("Failure") --prints "Failure"
  47. end --This ends the statement
  48.  
  49. --[[
  50. Lets translate the above into sudo-code (aka somewhat english)
  51.  
  52. Translated:
  53.  
  54. If 125 is higher than 500, then make a print with the value "Success"
  55. But if the above is false, then check if 325 is lower than 200, if yes, then
  56. make a print with the value "Success 2", If both of these conditions arent met,
  57. then make a print with the value of "Failure"
  58.  
  59. ^^^^^Make sure you speak your code as sudo-code when your experiencing a bug or issue
  60.  
  61. it helps alot
  62.  
  63. ----------------------------
  64.  
  65. These statements can have multiple conditionals as well
  66.  
  67. Example:
  68. ]]
  69.  
  70. if 5 > 2 and 5 < 10 then
  71.     print("Success")
  72. end
  73.  
  74. --[[
  75. By adding "and" to a conditional, it will add an another condition for the statement
  76. which makes the conditional require both conditions to be true for it to pass
  77.  
  78. However, if you want the code to have two seperate conditions and only one of them to pass to get
  79. the same result, you can add an "or"
  80. ]]
  81.  
  82. if 4 > 5 or 4 > 2 then
  83.     print("Success")
  84. end
  85.  
  86. --[[
  87. Adding an "or" will add a secondary condition, if the first conditon is a fail, then it checks
  88. if the second condition is met, if yes, then print "Success"
  89.  
  90. And if you are wondering, You can have both of these in the same conditional like this:
  91. ]]
  92.  
  93. if 3 < 8 and 3 > 2 or 5 > 2 then
  94.     print("Success")
  95. end
  96.  
  97. --And you can also have an another "if then" statement as well
  98.  
  99. if 2 > 1 then
  100.     print("Part 1")
  101.     if 5 > 2 then
  102.         print("Part 2")
  103.         if 13 > 9 then
  104.             print("Part 3")
  105.         end
  106.     end
  107. end
  108.  
  109. --[[
  110. And assuming you have this question, this is how an "if then" statement would work:
  111.  
  112. if (condition 1) or (condition 2) then
  113.  
  114. Lets move into something more exciting, Variables.
  115.  
  116. Variables can be added into "if then" statements
  117.  
  118. So you can do stuff like this:
  119. ]]
  120.  
  121. val1 = 12
  122. val2 = 9
  123.  
  124. if val1 > val2 then --12 is larger than 9, so it passes
  125.     val1 -= 5 --Reduces the value in val1 by 5, so now val1's new value is 7
  126.     if val1 > val2 then --Checks if val1 is above val2, But since val1's new value is 5, it doesnt pass
  127.         print("End")
  128.     end
  129. end
  130.  
  131. --[[
  132. now that should be "if then" statements completed i think.
  133. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement