• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rarely Asked Questions example: x=0; while(++x>0); WHY? D:  RSS feed

 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

So I was running through the Rarely Asked Questions (RAQ , question 7.) and I ran into this beautiful snippet of


And although I haven't graduated, the compiled code in my console said I did. So I spent countless hours thinking, and diving into javadocs, just to find out why. Well, I'm here because I've failed.

Would anyone be so kind and explain to my dumb head, why would b>0 evaluate before than ++b? Because docs say, that ++ evaluates first.
Now, if I assume that happens, it means that for some reason,  b>0 evaluates to 0>0 instead of 1>0. As far as I know, pre-increment operator work as:
- Increment input by 1.
- Return input

And post-increment work like:
- Store input
- Increment input by 1
- Return stored old input

If possible, I want the deepest explanation possible!

Thanks in advance, Dóri :3


UPDATE:
So I checked if I add a println to the loop, and it actually seems like the supposed-to-be-infinite loop terminates after printing out a lot of 3s. So its actually the JVM that terminates the loop early, after (probably) detecting it doesnt do anything good. Now I feel even worse
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dóra Takács wrote:
UPDATE:
So I checked if I add a println to the loop, and it actually seems like the supposed-to-be-infinite loop terminates after printing out a lot of 3s. So its actually the JVM that terminates the loop early, after (probably) detecting it doesnt do anything good. Now I feel even worse


Hint. What is the range of a byte? And what would happen when you add one to the maximum value?

Henry
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint. What is the range of a byte? And what would happen when you add one to the maximum value? 

   
It overflows into a negative QQ


Thank you!
... It couldn't be more obvious

I consider the question closed, and I'm gonna drink a coffee now.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have you worked out why you only get one congratulatory message, rather than 127 of them?

[edit]So the question isn't closed yet!
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have you worked out why you only get one congratulatory message, rather than 127 of them? 

Thats because we have an empty statement after the while ^^ a simple semicolon.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about this code? Remember that a char is unsigned and doesn't support negative values.I expect you will get the correct answer in a few seconds
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about this code? Remember that a char is unsigned and doesn't support negative values.

It would just bounce back to 0 instead of a negative number then, and 0>0 is false so the loop ends eventually, right?
 
Rahul kumar verma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Range of byte: -127 to 128

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul kumar verma wrote:Range of byte: -127 to 128

Afraid that is incorrect. The right answer is in the Java® Language Specification.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dóra Takács wrote:. . . the loop ends eventually, right?
The loop will run 65536 × and on the last time, there will be overflow from 65535 (= 0xffff) returning c to 0, so you are right. If you had written c++ the initial value would have been 0, so the loop would never had started. Change the empty statement to something different, e.g.
System.out.print(".");
and you can see the loop run.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final prints of Output:
65532: ?
65533: ?
65534: ?
65535: ?

I think initial value of h = '\0' is blank space because I tried to print that, it prints nothing. If I'm not wrong, here ++h > 0 compares ascii value of 0 i.e. 48 with all ascii values of special char comes by ++h which are larger than 48 when it reaches 65535 after that it becomes 0 so 48 > 48 returns false so loop terminated. Am I correct ?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:. . .I think initial value of h = '\0' is blank space because I tried to print that, it prints nothing. . . .
No '\0' is not a space. You can look in the Unicode chart (ASCII/Basic Latin) to see what it really is. You are printing a space after the colon, then the command line cannot cope with most of the characters and prints ? A Linux terminal will print many more characters than a Windows® command line, which is notorious for the restricted range of characters available.
Where did you get 48 from? There is no 48 in the code. You are looking for > 0. You wrote 0 not '0'. Try changing the code to use '0' and see what happens.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You wrote 0 not '0'
Opps!   ,yes ASCII value of '0' char is 48 but it is 0 i.e. int whose ASCII value is 0030 given in you pdf link but I couldn't find what is '\0'
Output: Out of while loop
then the command line cannot cope with most of the characters and prints ?
Oh I see, I was also thinking why it kept printing ? so many times.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0 is 0.
It is the NUL character.
As is '\0'.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:0 is 0.
It is the NUL character.
As is '\0'.
Thank you, is there any website where I can get the list of ASCII values. Where did you get that '\0' is NUL character. I used that in C language as end of string while doing string programs, where string is an array of character in C.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the standard escape code for null:
https://en.wikipedia.org/wiki/Null_character#Representation
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:It's the standard escape code for null:
https://en.wikipedia.org/wiki/Null_character#Representation
Thank you once again
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should have said "it's a standard escape code..."
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:. . . Thank you, is there any website where I can get the list of ASCII values. . . .
Hundreds of websites, including the official Unicode site I posted a link to this morning.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was pleased to see they have ctrl‑shift‑P as a way to get a null character from the keyboard. I remember using ctrl‑shift‑P‑repeat to get blank paper tape out of a Teletype.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!