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):
if (myNum<20) myNum++;
else myNum=0;
Method 2 (modular arithmetic):
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?