Pergunta

local times=0
function rTA(v)
  times=times+1
  if times % 3 <= 0 then 
        print(v)
    end
end

or

local times=0
function rTA(v)
  times=times+1
  if times == 3 then 
        print(v)
        times=0
    end
end

rTA("N1")
rTA("N2")
rTA("N3")
rTA("N4")
rTA("N5")
rTA("N6")
rTA("N7")
rTA("N8")
rTA("N9")

They both return the same output (N3, N6, N9), but I can't seem to understand the difference in both of them..

Foi útil?

Solução

As pointed out both are checking if "times" is multiple of 3, although the first version is a little more "elegant" it costs more in terms of processing. The second is a little less readable in terms of meaning (you can understand that it is trying to check for multiples of 3, but it is not a first sight thing, you have to think for a moment).

Cheers

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top