Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused point about infinite loop

 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Both of two loops are infinite. The statement which comes after 1st loop does compile but the second print statement doesn't compile, it is unreachable statement because infinite loop. It is interesting that the first loop is also infinite and there is one difference between 1st and 2nd loop. In first loop we check the value of variable b. But the condition is always true in any case in 1st loop although the value of b is true or false. Why does the compiler know the 2nd loop as infinite loop but 1st not?
 
Tim Cooke
Sheriff
Pie
Posts: 3203
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:

Mushfiq Mammadov wrote:In first loop we check the value of variable b.
No you're not. A single equals (=) is an assignment. Double equals (==) is an equality check.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start with a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code.

For the 1st loop, the assignment is done in the while loop, so that line should be executed before the compiler would know it's always true. In the 2nd loop, the compiler knows without executing any code that will run forever and the statement after the while loop will be unreachable.

Notice the difference between the following code snippetsAdding 6 characters gives a completely different result

Another similar example

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another topic about unreachable code with some examples.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here is another one with try-catch-finally blocks
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
Mushfiq Mammadov wrote:

Mushfiq Mammadov wrote:In first loop we check the value of variable b.
No you're not. A single equals (=) is an assignment. Double equals (==) is an equality check.

Sorry, maybe I explain my opinion wrongly for my poor English. I know it, I didn’t mean it when I said “check”. Sorry again
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Let's start with a very, very, very important rule: The compiler doesn't execute any code! So every compiler error you get, is because the compiler knows something is wrong without executing any line of code.

It is my paradox that if I read more I confuse more The problem is that sometimes I can’t determine which code is known without executing or which code with executing by compiler. I saw some codes when I practice tests. And I compare this example with those codes so I am confused. For example, I know such we can’t use local variable without initialization:
P.S. Just I test it and saw that if we declare i as final this code does compile

But we add else statement compiler know that s always initialize.

Another example:
The compiler also know the above code doesn’t compile.

In above examples confuse me and I think that compiler know the value of b ( while(b=true) ) is always true (according assignment) without executing.

Roel De Nijs wrote: For the 1st loop, the assignment is done in the while loop, so that line should be executed before the compiler would know it's always true. In the 2nd loop, the compiler knows without executing any code that will run forever and the statement after the while loop will be unreachable.

After the underlined sentence I understand that the compiler know it after executing.


Roel De Nijs wrote:Here is another topic about unreachable code with some examples.
Roel De Nijs wrote:And here is another one with try-catch-finally blocks

Thanks a lot for links, the posts are very helpful, you explain everything perfectly, especially difference between while and if. The following example is very interesting and tricky:
System.out.println("after loop"); isn’t unreachable according break. But break is also unreachable although does compile.

I confuse in one point then I understand.
If you would comment the finally block, 4 is still unreachable because of 3.

At first I don’t understand it, then I think that maybe you want to say because of 2. Do I think your intention correct or not?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:The problem is that sometimes I can’t determine which code is known without executing or which code with executing by compiler.

Does compile time constant sound familiar? If you have a compile time constant, the compiler knows the value; otherwise it knows nothing at all

Mushfiq Mammadov wrote:In above examples confuse me and I think that compiler know the value of b ( while(b=true) ) is always true (according assignment) without executing.

b will only be true if the assignment is executed. Without the execution of the assignment, the value of b is not known! And the compiler doesn't execute code, so the compiler has completely no idea about the value of b.

Mushfiq Mammadov wrote:System.out.println("after loop");[/tt] isn’t unreachable according break. But break is also unreachable although does compile.

The compiler sees the if statement and the break statement, so it knows there is a condition the (endless) while loop will be exited when this condition is met. And you know from previous examples that the compiler doesn't care about if statements. So that's why the code compiles.

Mushfiq Mammadov wrote:At first I don’t understand it, then I think that maybe you want to say because of 2. Do I think your intention correct or not?

You are correct! That was a typo of mine. Well spotted! I fixed it in the original post, so others don't get confused (and I gave you credits for reporting)

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Does compile time constant sound familiar? If you have a compile time constant, the compiler knows the value; otherwise it knows nothing at all

Yes. When I encounter similiar case I will think about compile time constant at first.

Roel De Nijs wrote:and I gave you credits for reporting

Woow Thank you for your credit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic