While loop output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loop output.

What is the output of this code? byte by = 0; while(by > 0) by++; System.out.println(by); Why is it equal to -128?

24th Aug 2019, 3:32 PM
Boubacar Toure
Boubacar Toure - avatar
2 Answers
+ 1
Because of the limits of the type byte: between -128 and 127. The last step when variable by is positive is 127, after incrementing it becomes -128 and the loop stops.
24th Aug 2019, 3:49 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
The output of your code ist 0. But if you modify the while condition to (by >= 0), it is -128.
24th Aug 2019, 4:14 PM
Michael
Michael - avatar