*
The moose likes Beginning Java and the fly likes Which is correct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which is correct" Watch "Which is correct" New topic
Author

Which is correct

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
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

Joined: Mar 02, 2008
Posts: 87
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.


Imagination is more important than knowledge "Albert Einstein"
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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."


[Jess in Action][AskingGoodQuestions]
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks

just a little more clarification on the +=

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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
So b += a

would be b = b + a then?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41125
    
  45
Yes.


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: Which is correct
 
Similar Threads
Exeception in thread
Exception in thread
Output errors in main
cannot find symbol
might not have been initialized