THEeric
|
  |
| Joined: 08 May 2008 |
| Total Posts: 6185 |
|
|
| 16 Feb 2012 10:10 AM |
Say I have this for loop:
for i=1,11 do something blah something end
and I want to put in an if statement making a certain part of the script only run when 'i' is an odd number, what do I need to do? Is there a built in function for that? |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 16 Feb 2012 11:00 AM |
No, there isn't a function to check whether a number is odd or even. But, seeing as all even numbers are divisible by 2, you can use the modulus operator % to check if the remainder is 0.
if number % 2 == 0 --checks if 'number' divided by 2 has a remainder of 0 (all even numbers would) |
|
|
| Report Abuse |
|
|
THEeric
|
  |
| Joined: 08 May 2008 |
| Total Posts: 6185 |
|
|
| 16 Feb 2012 11:05 AM |
| Huh, ok, thanks. That would be much better than having to write out a bunch of odd numbers in a table or do anything messy like that. |
|
|
| Report Abuse |
|
|
|
| 16 Feb 2012 11:06 AM |
Yeah, And you could do something like
function isOdd(number) if (number % 2 ~= 0) then return true else return false end end |
|
|
| Report Abuse |
|
|
THEeric
|
  |
| Joined: 08 May 2008 |
| Total Posts: 6185 |
|
|
| 16 Feb 2012 11:07 AM |
| Just messed around wit hit in command, ok I see how it works. Thanks! |
|
|
| Report Abuse |
|
|