• Post Reply Bookmark Topic Watch Topic
  • New Topic

Kindly check below program and let me know why it is printing big x.  RSS feed

 
Vikram Munjal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What else would you expect the program to do?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: What happens, if you keep subtracting from an integer, that it can't get any smaller?
 
Vikram Munjal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:What else would you expect the program to do?


As Print statement is in If loop and x is never going to be greater than 3. So, I am expecting it not to print anything.
Kindly correct me if i am wrong.
 
Charles Nduka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You created problems for yourself by  "while ( x < 10) {}" and "x = x - 1;". whats the loop and statement for?.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the value of x to the print statement so you can see what the value in x has become.
 
Mani pradeepmari
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Add the value of x to the print statement so you can see what the value in x has become.


It will print 2147483647. It is the maximum value for int.  When it reaches the Max value it will print the "big X"
 
Vikram Munjal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Nduka wrote:You created problems for yourself by  "while ( x < 10) {}" and "x = x - 1;". whats the loop and statement for?.


Hi Charles, I am practicing the Java code and i am looking for a clarification. So, Kindly help me with the clarification.
 
Charles Nduka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just delete the lines i mentioned and then run to give you your desired result
 
Henry Wong
author
Sheriff
Posts: 23295
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
Charles Nduka wrote:just delete the lines i mentioned and then run to give you your desired result


I think you are missing the point. The OP isn't just looking for a desired output. The OP is wondering why the code gives an unexpected (to the OP) output.

Henry
 
Charles Nduka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program has lots of background processes which makes it much more complicated than you know. kindly modify or simplify for you own sake.........
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Charles!

As Henry pointed out, this is probably just an example program to explain some concepts. The goal is not to edit it, but to understand it.
 
Vikram Munjal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Nduka wrote:Your program has lots of background processes which makes it much more complicated than you know. kindly modify or simplify for you own sake.........


Hi Charles,
I printed the value of x and that came out to be more than 3. This helped in understanding the process.
I really appreciate your help with this. Thanks a lot for your support.
 
Frankie Law
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikram,

I don't know if this will help or not, nor whether or not this is the problem.

It seems that your program is running an infinite loop since x is a negative number. 
Therefore, the while loop will never end.
Then you got an if statement that checks if x will be greater than 3

I assume that your computer ran an infinite loop of x = x - 1;  which exhausted the memory of what "int" is capable of handling.

If you run into the number 2,147,483,647 which looks a lot like the number that "int" can handle. 
When the memory of int had exhausted, it simply gave you the largest number it can handle. 

Therefore, it display "Big X"

I remember running into this problem back when I was learning C.
Check this page out for what other more experience programmer has to say about it.

http://stackoverflow.com/questions/17218964/often-big-numbers-become-negative
 
Vikram Munjal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankie Law wrote:Hi Vikram,

I don't know if this will help or not, nor whether or not this is the problem.

It seems that your program is running an infinite loop since x is a negative number. 
Therefore, the while loop will never end.
Then you got an if statement that checks if x will be greater than 3

I assume that your computer ran an infinite loop of x = x - 1;  which exhausted the memory of what "int" is capable of handling.

If you run into the number 2,147,483,647 which looks a lot like the number that "int" can handle. 
When the memory of int had exhausted, it simply gave you the largest number it can handle. 

Therefore, it display "Big X"



I remember running into this problem back when I was learning C.
Check this page out for what other more experience programmer has to say about it.

http://stackoverflow.com/questions/17218964/often-big-numbers-become-negative


Hi Frankie,

You are correct.
I printed the value of x and shows the highest number int can store.
That's why it is printing big X.
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankie Law wrote:. . .
It seems that your program is running an infinite loop since x is a negative number. 
. . .
I assume that your computer ran an infinite loop of x = x - 1;  which exhausted the memory of what "int" is capable of handling.
I am afraid that is not correct. There is no infinite loop, and the loop will definitely trminate.
If you run into the number 2,147,483,647 which looks a lot like the number that "int" can handle.  . . . http://stackoverflow.com/questions/17218964/often-big-numbers-become-negative
You might do well to search a bit more, because I don't think the explanations on that SO thread are really good. I like the sixteen numbers in a circle (which I have seen elsewhere) however. Search for overflow error, which is what is happening. It is a standard problem with all computerised representations of integers. If you keep subtracting 1, you will eventually reach −2147483648 (= −2³¹) which is the most negative number an int can represent. If you subtract 1 from it, you will suffer an overflow error and get (+)2147483647, which satisfies the > 3 test. You can get similar overflow but in the other direction if you add to 2147483647.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!