This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes The use of brackets in mathematical equations. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The use of brackets in mathematical equations." Watch "The use of brackets in mathematical equations." New topic
Author

The use of brackets in mathematical equations.

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85


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

Joined: Jan 28, 2008
Posts: 5575

WeiJie Lim wrote:
The above doesn't work

what is the error message?
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
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

Joined: Apr 06, 2010
Posts: 4377
    
    8

And remember that this is integer arithmetic. What is 1/2?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 05, 2012
Posts: 85
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 02, 2003
Posts: 11257
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Sep 05, 2012
Posts: 85
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: The use of brackets in mathematical equations.