+ 6

Do the Mathematics in Java Programming overrides the rules of Mathematics?

I have got a rare situation while trying out some examples of infinite loop of my own and found a great confusion for the Mathematicians. Just go throughout the code given below and give the reason why the loop seems to infinite (In the View of Maths) but a finite one. https://code.sololearn.com/cQovo1JMups2/?ref=app

22nd Oct 2018, 11:18 AM
Sujal Kumar
Sujal Kumar - avatar
6 Antworten
+ 6
x will take the values 0-127 (that's the capacity of positive values a signed byte can hold), followed by -128 (that's probably because of an overrun because you keep incrementing the byte value beyond its capacity) to -1. So the loop will be executed 255 times. The same will happen with an integer eventually (in a much larger range of values).
22nd Oct 2018, 11:39 AM
Anna
Anna - avatar
+ 6
Anna Congratulations! You got it perfectly. I already know the answer but that's a small test for my friends on SL. Well done!
22nd Oct 2018, 11:46 AM
Sujal Kumar
Sujal Kumar - avatar
+ 5
Anya You are right but not exactly but I would appreciate your try.
22nd Oct 2018, 11:33 AM
Sujal Kumar
Sujal Kumar - avatar
+ 4
Anna I imagine that. But your explanation is perfect.
22nd Oct 2018, 11:43 AM
Anya
Anya - avatar
+ 2
Oh. Never Seen that in a code. But I already know what just happened. Just int instead byte. And see a surprise.
22nd Oct 2018, 11:27 AM
Anya
Anya - avatar
0
yes
22nd Oct 2018, 10:02 PM
Waddah Yousef Mohammed El hassan
Waddah Yousef Mohammed El hassan - avatar