Given the following code, why won't it work? Write one line to fix it int main() { uint8 x = 0; while (x < 255) { x = x + 2; print(x); // yes I know this is wrong } }
Sigiloso
It wont work because uint8 takes up 255 vaue, When u increment from 0, x becomes 254 i the last round where x =x+2 happens and X's final value will become 256 and there is an overflow in the variable. One thing which we can do is increase the variable size to uint16. or since its only one value 256, we can have an if check for x=254 and not add x= x+2 something like this: while(x<255){ if(x ==254){ print("256"); }else{ x=x+2; print(x); } }