简体   繁体   中英

Java while(condition doesn't reach)?? Why

Why is it when I do this

while(word.charAt(left) == word.charAt(right) && left >= 0 && right < word.length()){
            left--;
            right++;
        }

its says in the while conditions "left >= 0 && right < word.length()" condition doesn't reach, but when I do it like this

 while(left >= 0 && right < word.length() && word.charAt(left) == word.charAt(right)){
            left--;
            right++;

        }

The conditions are met. Is there an order for while(loop conditions)???

The order does matter as conditions are checked from left to right. You're getting that message because if the previous condition evaluates to true, then those two conditions

left >= 0 && right < word.length()

will always evaluate to true. You probably want the latter (since you would want to check all the conditions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM