Sunday 15 May 2011

In the Head First C# book, I cannot understand why 2 ifs for checking the same condition again -



In the Head First C# book, I cannot understand why 2 ifs for checking the same condition again -

i'm not able understand why 2 times same if status checked when goal homecoming 0 if amount not > 0

i found code on page 737 of head first c# book.

can explain?

public int receivecash(int amount) { if (amount > 0) { if (amount > 0) { cash += amount; homecoming amount; } console.writeline("{0} says: {1} isn't amount i’ll take", name, amount); } homecoming 0; }

just guess (based on op's comment else beingness default), code should this:

public int receivecash(int amount) { if (amount > 0) { cash += amount; homecoming amount; } else { console.writeline("{0} says: {1} isn't amount i’ll take", name, amount); homecoming 0; } }

here if amount greater 0 it's added cash variable , amount added returned, otherwise (else) error message printed console , 0 returned.

not illustration of if-else in opinion, don't have book don't have context of exmaple, code.

c#

No comments:

Post a Comment