modulo - %
Know how to use the modulo operator.
Modulo means 'remainder after division'. The value of 11 % 5 is therefore 1, because 5 goes into 11 twice ( 5*2 == 10 ) but there is a remainder of 1 to make 11.
It is useful to use modulo to cycle between options. In the Processing example below, modulo is used to cycle through 3 different colors used in the fill statement.
It is useful to use modulo to cycle between options. In the Processing example below, modulo is used to cycle through 3 different colors used in the fill statement.
for (int i = 0; i<20; i++){
if (i%3==0) {
fill(255,0,0);
}
else if (i%3==1){
fill(0,255,0);
}
else if (i%3==2){
fill(0,0,255);
}
ellipse(i*10,i*10,10,10);
}
In the above method, the code will draw a row of circles (ellipses) but will use modulo to decide if the color of each circle is red, green or blue.