# Efficiency: if-else vs modular arithmetic

**URL:** <https://forum.kirupa.com/t/efficiency-if-else-vs-modular-arithmetic/272617>\
**Category:** flash\
**Created:** [October 7, 2008, 2:19am UTC](https://forum.kirupa.com/t/efficiency-if-else-vs-modular-arithmetic/272617 "2008-10-07T02:19:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iworms](https://avatars.discourse-cdn.com/v4/letter/i/919ad9/32.png) [@iworms](https://forum.kirupa.com/u/iworms)\
**Post date:** [October 7, 2008, 2:19am UTC](https://forum.kirupa.com/t/efficiency-if-else-vs-modular-arithmetic/272617/1 "2008-10-07T02:19:13Z")

</div>

Hi everyone, I don’t have the computer science knowledge to figure out this question: Suppose each time a button is clicked, a number (say **myNum** ) gets incremented. But when it gets to, say, 20, I want it to loop back to 0. I wonder which of the following ways is more efficient:

Method 1 (if-else):

```auto
if (myNum<20) myNum++;
else myNum=0;

```

Method 2 (modular arithmetic):

```auto
myNum = (myNum+1) % 20;

```

Both are the same functionally, and method 2 is cleaner for the eye. But when executed, which one is more efficient and faster?
