Barnabybear wrote:Mmiscool wrote:eduperez wrote:I would also consider using a switch / case statement: https://www.arduino.cc/en/Reference/SwitchCase
Hi, thanks. As it sets a variable for the number of times a for loop circles (its a lighting effet that 50% of the time runs the full effect, 34% and 14% runs a shortened effect). Is there an advantage to switch case over "if and else if"? Or generaly a better way to do this?Code: Select allrLen = (random(6));
if (rLen == 0 || rLen == 1 || rLen == 2 ){
nZone = 17;
}
else if (rLen == 3 || rLen == 4){
nZone = 16;
}
else {
nZone = 15;
{
for (int Zone = 12; Zone <= nZone; Zone++) {
Well, there is a semantic difference: "switch" is more suited when you have to choose an action based on the many values of a variable. For example, I would have done something similar to this (bear in mind that this is untested code):
switch (random(6)) {
case 0:
case 1:
case 2:
nZone = 17;
break;
case 3:
case 4:
nZone = 16;
break;
default:
nZone = 15;
}