简体   繁体   中英

What does this code tell computer to do lights[k] = !lights[k];?

what does this code tells the computer to do? Ps The 'lights' is a boolean array.

for (int k = 1; k < lights.length; k++)
                lights[k] = !lights[k];

It's basically toggling the boolean flags in the array (except the first one). A true flag will be set to false and vice-versa.

Note that any uninitialized items of a boolean array will be false in Java.

 const lights = [false, false, false]; //toggling flags except the first one for (let k = 1; k < lights.length; k++) lights[k] = !lights[k]; console.log(lights); 

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