Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is correct

 
Richard Chambers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and yet another basic, I'm a slow learner question.

Which of the two are right, or does it matter. And if it does matter what is the guideline.

the pseudocode is:





or



The sumOfBeginningBalances is the variable I'll be working with in this method.

Thanks
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it depends on whether you're updating sumOfBeginningBalances or the method currentAccount.getBalance(). what preceded this in the pseudocode? this will affect what you'll do. my gut suggests your second option but w/o the benefit of the specifications it is only a guess.
 
Richard Chambers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the million dollar question.

In the application I'm working with multiple accounts, and the sumOfBeginningBalances it the total for all accounts. In this method, I'm working with only one of those accounts.

I'm "guessing" I'm updating the sum.

I'm sure I'm microanalizing too much. Until I get the beginningBalance it's initialized at 0.0, so it updates, but the sum is also 0.0 until it updates the first time.


This is the first line of this method BTW.

Also, I'll throw in another question, for when I get this done.

In a conditional coding, if I want both conditons to be true before it does the statement that's "&"?

& checks both conditions?
&& stops if first is false?

Thanks
[ April 13, 2008: Message edited by: Richard Chambers ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first question, only the second one is legal Java. The left operand of the "+=" operator must be a variable. Since you've got a sum, and want to add more to it, then the second one is not only the legal one, but also the one that makes sense.

For the second question both operators will do the right thing, but "&&" will do less work in some cases. For both of these, the controlled statement will only be executed if both the conditions are true. The difference is that "&" will always execute both conditions. The "&&" operator will say "Hey, the first condition was false, so I don't care what the second one evaluates to, since it doesn't matter; I'm not going to execute the statement no matter what, so I'll just skip it."
 
Richard Chambers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

just a little more clarification on the +=

if I'm using two variables, which one updates,
the left or right.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Richard Chambers:
...just a little more clarification on the +=

if I'm using two variables, which one updates, the left or right.

a += b is just shorthand for a = a + b.
 
Richard Chambers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So b += a

would be b = b + a then?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic