This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The use of brackets in mathematical equations.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above doesn't work,

But the code below with the brackets surrounding the whole right hand side works. Why ? I thought for b = b-(b/2); Java will just execute the brackets 1st (b/2) , and then execute the b which is outside the brackets ?


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:
The above doesn't work

what is the error message?
 
simon fletcher
Ranch Hand
Posts: 50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If what you are trying to do is output all numbers from 16 down that can be divided by 2 then your problem lies in the line of code that is in the brackets beside the keyword 'while'. I ran the code the way you wrote it and with both examples I got an endless output of 1's. Kind of hypnotizing to watch. I made one tiny correction (as well as adding a closing bracket that your example is missing, probably from when you copied and pasted) and both examples output 16 8 4 2. I don't see how your second example could have worked the way you show it. Unless you wanted to see a hynotizing screen full of ones, which is not a bad thing. Anyway, look at the argument that controls your while loop.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember that this is integer arithmetic. What is 1/2?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:

The above doesn't work,

But the code below with the brackets surrounding the whole right hand side works.


Nope. Both versions behave identically, as you would expect. If you're seeing different behavior, you changed something else.

Also, in the future, please remember that ItDoesntWorkIsUseless(←click), so you need to TellTheDetails(←click).
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I think there are mistakes somewhere.. Somehow I got the 2nd example to run back then.

I am expecting a printed value of "16 8 4 2 1" . But all it prints out now is a string of 1s, like another user mentioned.

I don't get why b= b-(b/2); will give a string of 1s..

My train of thought :

int b = 16;

b = b-(16/2) ;

b = b-(8);

b = 16-8;

b= 8 ;
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hint: what is the current updated value of b in second iteration and so on ?

<edit>
WeiJie Lim wrote:
But all it prints out now is a string of 1s, like another user mentioned.

that's probably because of seeing console tail... yes, after few iteration you can see printing 1 i.e, 2 - (2/2) .
here note loop is endless because of b = 1;
</edit>
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a BIG fan of using System.out.println() statements to see what is going on. you can do stuff like this:


This lets you see when you start each loop, what b is getting set to, when you finish each iteration of the loop, etc.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:hint: what is the current updated value of b in second iteration and so on ?

<edit>
WeiJie Lim wrote:
But all it prints out now is a string of 1s, like another user mentioned.

that's probably because of seeing console tail... yes, after few iteration you can see printing 1 i.e, 2 - (2/2) .
here note loop is endless because of b = 1;
</edit>


Yup "after few iteration you can see printing 1 i.e, 2 - (2/2) ."

But using another formula-> b = (b/2) ; also ends up with b =1; , but it doesn't have an endless loop . Why ? =/

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:
But using another formula-> b = (b/2) ; also ends up with b =1;


No, it doesn't.

The last thing you see is b=1. Then we do b = b/2, which gives b = 0, we go back to the top of the loop, and the b >= 1 test fails, so the loop body isn't executed.

If you print out b after the loop, you'll see 0.

For the loop to end, the while (b>=1) condition has to become false. That means b has to be 0 or less, and it does get to 0 at b=1/2.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shows how you can get confusion with the >= and <= operators. If you use < and > alone, they are easier to understand.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
No, it doesn't.

The last thing you see is b=1. Then we do b = b/2, which gives b = 0, we go back to the top of the loop, and the b >= 1 test fails, so the loop body isn't executed.

If you print out b after the loop, you'll see 0.

For the loop to end, the while (b>=1) condition has to become false. That means b has to be 0 or less, and it does get to 0 at b=1/2.


Oh thanks I think I get it. For the formula b = (b-(b/2)) , it will be stuck in an infinite loop when b=1 as b = (1-(1/2)) = (1-0) = 1 .

Is this the reason ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:[
Oh thanks I think I get it. For the formula b = (b-(b/2)) , it will be stuck in an infinite loop when b=1 as b = (1-(1/2)) = (1-0) = 1 .

Is this the reason ?


Well, yes, it's true that b = (b-(b/2)) when b is 1 is b = (1-(1/2)) = (1-0) = 1. As to whether that causes an infinite loop or not, it depends on your loop condition. In your particular, case, since your loop condition was while (b >=1), yes, that combination led to an infinite loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic