Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+17 votes
asked in CSC211_Winter2018 by (1 point)

1 Answer

+7 votes

Yes, it's pretty simple, but the system just crashes, here is an example:

for (int infinite = 1; infinite >= 1; infinite++)

This would be an infinite loop.

answered by (1 point)
+6

You would certainly think that the code you wrote would create an infinite loop, but strangely enough it does not. It turns out that integers in Java have a limited range. Once the loop variable reaches 2,147,483,647, when you add one more to it, the variable's value wraps around to a large negative number (-2,147,483,648), at which point the loop will stop.

However, it is quite possible to create an infinite loop. Just change your update step from infinite++ to infinite=1, to keep setting the variable back to 1.

...