Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How for( ; ;) is infinite loop? [duplicate]

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.

like image 719
KKK Avatar asked Feb 20 '14 17:02

KKK


People also ask

How many times will an infinite loop repeat?

As we already know that non-zero integer represents the true condition, so this loop will run infinite times.

Is infinite loop possible in for loop?

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.

Does an infinite loop ever stop?

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").


2 Answers

According to Java Language Specification, section 14.14.1.2:

for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement

If the Expression is not present, or it is present and the value resulting from its evaluation (including any possible unboxing) is true, then the contained Statement 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.

like image 134
Sergey Kalinichenko Avatar answered Oct 04 '22 20:10

Sergey Kalinichenko


You do not specify any condition to continue the loop, so it is executed forever.

like image 28
Kapol Avatar answered Oct 04 '22 20:10

Kapol