Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. Chapter 4: For Loops
  2.  
  3. What is a for loop?
  4.  
  5. It's exactly what it sounds like. It loops over and over and over.
  6. We just covered tables. Well loops are how you travel through those tables.
  7. There are 3 different types of for loops and each one has it's own special use.
  8. Keep in mind there are 2 more 'special' loops that won't be covered in this section.
  9. These 'special' loops will be covered later since they are not 'for loops'
  10.  
  11. You know you're dealing with a for loop when you see the following word:
  12. [code=lua]for[/code]
  13.  
  14. Simple so far huh? Let's keep going ;)
  15.  
  16. To truly understand loops, you have to understand the table you are trying to loop through.
  17. I hope you paid attention to the previous chapter. The structure of the table you are looping through is crucial.
  18. To start off, we will begin with the generic loop that can be used for any table structure.
  19.  
  20.  
  21. The Generic Loop
  22. [code=lua]for i = 1, 10 do
  23. print(i)
  24. end[/code]
  25. Prints 1,2,3,4,5,6,7,8,9,10 to the screen.
  26.  
  27. Dafuq, I thought you said we were going to loop through a table?!?
  28. Well it turns out this loop is sooo generic, that you actually can use it to simply.. loop however many times as you want.
  29. This loop has tons of uses, but the structure is always the same:
  30.  
  31. [code=lua]for variable = startNumber, endNumber do
  32. -- code
  33. end[/code]
  34. The variable mentioned simply holds the current number inside the loop. In the earlier example, this was the letter 'i'.
  35. Each time we loop, the variable updates to the next number.
  36.  
  37. Now you may or may not be thinking.. Does it always have to be 1 through 10? NO! It doesn't.
  38.  
  39. [code=lua]for i = 5, 50, 5 do
  40. print(i)
  41. end[/code]
  42. Prints 5,10,15,20,25,30,35,40,45,50 to the screen.
  43.  
  44. Hold on a sec buddy...you added an extra number. Why yes I did. If you didn't guess from the output, this third number specifies what to increment by.
  45. Usually it just increments by 1, however if we specify anything else, it will increment by that much.
  46.  
  47. So aren't we supposed to be looping through tables or something? Yes we can with this loop.
  48. [code=lua]local myTable = {'a','b','c','d'}
  49. for i = 1, #myTable do
  50. print(myTable[i])
  51. end[/code]
  52. Prints a,b,c,d to the screen.
  53.  
  54. What is #myTable? This is simply how you get the size of a table. Since we have 4 variables in 'myTable' when we use #myTable, it's returning 4.
  55. So basically our loop is: 'for i = 1, 4 do'. Cool huh?
  56. Now... what about 'myTable[i]'? Since 'i' holds the number we're on, it's simply: 'myTable[1],myTable[2]...'
  57. This is how we loop using The Generic Loop. The next loops are for tables only.
  58.  
  59. The Pairs Loop
  60.  
  61. What's a pairs? Hah good question. It's variable that has a 'key'.
  62. Okay... what's a 'key'. Alright buddy, let me just show you.
  63.  
  64. [code=lua]local myTable = {user='Syntax',password='123456'}[code]
  65.  
  66. Alright so 'user' would be the key and 'Syntax' would be the value. Same for password and 123456.
  67. If you don't specify a key, the index is a number (how we just accessed them in the last loop)
  68.  
  69. WAIT! If they have a key, do they get an index number? No. This is why we cannot use The Generic Loop for key,value tables.
  70. However don't fret, it's the same exact way to access, just with a string!
  71. [code=lua]myTable['user'][/code]
  72. or
  73. [code=lua]myTable.user[/code]
  74.  
  75. Either way will work. The second way is good if you don't have spaces or special characters.
  76. So this is the loop that makes these magical tables it's bitch.
  77.  
  78. [code=lua]for k,v in pairs(myTable)
  79. print(k .. ': ' .. v)
  80. end[/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement