views:

171

answers:

4
for (m = 0; m < troopsCount.length; m++) {

                //FM_log(7,"i="+i+" m="+m);
                //FM_log(7,"tipoTropaPrioritaria[m] = "+tipoTropaPrioritaria[m]);
                //FM_log(7,"troopsCount[m] = "+troopsCount[m]);
                //FM_log(7,"availableTroops[m] = "+availableTroops[m]);

                if ((tipoTropaPrioritaria[m] == null || tipoTropaPrioritaria[m] == "undefined")
|| (troopsCount[m] == null || troopsCount[m] == "undefined") ||
(availableTroops[m] == null || availableTroops[m] == "undefined")) 
                    return "alternaTropas(): ERRO - tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m] null ou undefined";

                if ((parseInt(tipoTropaPrioritaria[m]) != 0) && (parseInt(troopsCount[m]) != 0)) {
                    naoServe = true;
                    break;
                }
                else {
                    if ((parseInt(availableTroops[m])) < (parseInt(troopsCount[m]))) {
                        naoServe = true;
                        break;
                    }
                    else if (m < troopsCount.length) {
                        naoServe = true;
                    }
                    else { //means m >= troopsCount.length
                        naoServe = false;
                    }
                }
}

my question is: the last statement

else { //means m >= troopsCount.length
    naoServe = false;
}

will it ever be evaluated since

for (m = 0; m < troopsCount.length; m++)

???

+2  A: 

No, it won't be executed, assuming that m and troopsCount aren't modified in the loop itself (which in this example they don't seem to be).

As I believe you're pointing out, the loop's conditional would prevent the loop from running again if m were greater than or equal to troopsCount.length at the start of the loop.

Syntactic
...or that `troopsCount` isn't modified either.
Joey Adams
@Joey Adams: Good point! Fixed.
Syntactic
A: 

Nope. It should never happen.

The loop stops immediately once m < troopsCount.length is false. As such, m >= troopsCount.length will never be true inside the loop, unless you change its value inside the loop itself (which you don't, in this sample).

Matchu
A: 

No. The loop is only evaluated as long as m < troopsCount.length. So m will never be >= troopsCount.length as long as you don't modify m or troopsCount.length inside the loop.

Douwe Maan
A: 

let´s assume troopsCount.length = 10

when m = 9 it will execute all the code in the loop right, but when m = 10 it won´t execute anything.

so if I change it this way:

            else {
                if ((parseInt(availableTroops[m])) < (parseInt(troopsCount[m]))) {
                    naoServe = true;
                    break;
                }
                else if (m < (troopsCount.length - 1)) { // troopsCount.length - 1 = 9, m < 9 = m from 0 to 8
                    naoServe = true;
                }
                else { // troopsCount.length = 9
                    naoServe = false;
                }
            }
        }

it would work, right?

Fernando SBS
You should probably edit (or add comment to) your original question instead of posting this question as an answer.
bn
since i´m assuming another condition (troopsCount.length = 10), I find it more stylish to post an answer to my question, since it is indeed an answer to my question, just waiting for someone with more knowledge than me confirm if my tought is right.
Fernando SBS