Like many other question explained that while(true) {}
is an infinite loop and so is for( ; ;)
my question is while(true)
makes sense the conditions is always true but there is no vivid condition true/false
in for( ; ;)
so how is the later an infinite loop.
As we already know that non-zero integer represents the true condition, so this loop will run infinite times.
Let us see some examples of how we can run into infinite loops. One of the most common infinite loops is when the condition of the while statement is set to true. Below is an example of code that will run forever. Another classic example will be of the for loop where the terminating condition is set to infinity.
In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs ("pull the plug").
According to Java Language Specification, section 14.14.1.2:
for ( ForInit
opt; Expression
opt; ForUpdate
opt) Statement
If the
Expression
is not present, or it is present and the value resulting from its evaluation (including any possible unboxing) istrue
, then the containedStatement
is executed.
Since the standard treats missing expressions and expressions evaluating to true
in the same way, the for
loop with the missing expression is equivalent to an infinite loop.
You do not specify any condition to continue the loop, so it is executed forever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With