Login to WatchAwear

15
Vote

The % Operator

This is probably the most useful but underused operator. It very often happens that we want something to count up... but only to a certain point, and then start again at the bottom. Like cycling through screens, or colors. I see lots of code with things like “var_x=var_x+1 ; if var_x==10 then var_x=0 end”... it's a waste. We have an operator that does this for us already.

The % sign is the modulus operator. Basically, A%B means “the remainder when A is divided by B”, or equivalently, “the number that you get when you subtract B from A repeatedly until you get a non-negative number less than B.” This is exactly what you need for this kind of thing:

var_x=(var_x+1)%10

This counts up from 0 to 9, and back to 0. In lua (unlike most other languages), we are frequently counting from 1 to n, not 0 to n-1, so the formula would be a little different for the 1 to n case:

var_x=var_x%10+1

Don't use those long-winded functions; it's just clunky.

Published by Mark Shoulson on 27 August 2015WatchMaker Tips & Tricks Posted

Download the
'WatchAwear - Resources for WatchMaker' App

  
WatchAwear - Companion for WatchMaker

Download The Best Watch Designing App
for Wear OS, and Tizen:

  
WatchMaker Beautiful Watches for Android Wear

No smartwatch required!!
Best clock wallpaper for Android!

  
WatchMaker Live WallPaper