int max_range = 100;
// I do not want to add more else if.. Actually max range is still 100 with more else ifs.
// Range can take values from 0 to 100
if (range <= 10){
a[0]= value;
}
else if (range > 10 && range <= 20){
a[1]= value;
}
else if (range> 20 && range <= 30){
a[2]= value;
}
else if (range > 30 && range <= 40){
a[3]= value;
}
else if (range> 40 && <= max_range){
a[4]= value;
}
It is simple code. I would like it to remove the nested else if's and use a for loop. How can I convert this into a for loop?
if-elseis one operation - what is yourforloop going to loop round?aI guess, but then you would still need some kind of condition for the range anyway. Would it be more readable and easier to understand? Probably not.iftoswitchis something I've seen, but it looks like you can make a loop that goes from0tomax_range / 10. The loop value (sayi) could be used as an index, likeif (range > i*10 && range <= i*10+10){ a[i] = value; }.