开发者

javascript for (i = 0; i < XXX.length; i++) -> length question

开发者 https://www.devze.com 2023-01-01 11:37 出处:网络
for (m = 0; m < troopsCount.length; m++) { //FM_log(7,\"i=\"+i+\" m=\"+m); //FM_log(7,\"tipoTropaPrioritaria[m] = \"+tipoTropaPrioritaria[m]);
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] || 开发者_StackOverflow社区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++)

???


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.


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).


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号