4
if() {

}else {
    if (IsAlternateRow=='true')
        IsAlternateRow = 'false';
    else
        IsAlternateRow = 'true';
}

Can I place an if and else statement inside of another else statement?

7
  • 4
    Try to post this question to the code review website (codereview.stackexchange.com). It's more suited there.
    – Hosam Aly
    Apr 11, 2011 at 15:17
  • yes, technically speaking you can do this, but you will want to avoid it whenever possible to keep your code 'clean' and readable Apr 11, 2011 at 15:17
  • Sidenote: if (IsAlternateRow == 'true') will certainly be false, as == evaluates object reference equality. If you wanted string comparison, use .equals().
    – wkl
    Apr 11, 2011 at 15:18
  • 1
    Well it's not longer Java but JavaScript,... which makes for a lot of confusion :D
    – Skurmedel
    Apr 11, 2011 at 15:23
  • 1
    Yeah, this question has significantly changed since it was created. First it was a "how can I change this code to make it better" and was tagged java. Now it's a "can I nest if/else statements" tagged javascript ...
    – Rob Hruska
    Apr 11, 2011 at 15:24

9 Answers 9

10

Yes, placing an if inside an else is perfectly acceptable practice but in most cases use of else if is clearer and cleaner. E.g.

if (test) {
   // Do something
} else if (otherTest) {
   // Do something else
} else {
   // Do a third thing
}

infact this is short-hand for

if (test) {
   // Do something
} else {
     if (otherTest) {
         // Do something else
     } else {
         // Do a third thing
     }
}

and the two should compile to almost identical programs in most situations.

Your code example is not very clear and will not compile correctly, clearer sample code may help us to help you out.

9

Note: the question got re-tagged as JavaScript after this answer was posted (it was originally about Java, so this answer is about Java).

In general, it is fine to place an if and an else inside an else clause.

There are, however, several issues with your code. IsAlternateRow=='true' isn't syntactically valid.

  • If it's a string comparison, you should use double quotes and .equals();
  • If it's a boolean comparison, you're better off simply doing IsAlternateRow = !IsAlternateRow instead of the entire nested if.
4
  • 3
    @Chris Nope. 'true' and 'false' are not valid character constants. Apr 11, 2011 at 15:20
  • @Isaac Truett - I upvoted your comment, but the question has changed tags from java to javascript, so it no longer applies. We all got pwned by late editing :(
    – Rob Hruska
    Apr 11, 2011 at 15:26
  • @Issac Why aren't 'true' and 'false' valid character constants. Apr 11, 2011 at 15:26
  • @Ash In Java, single quotes wrap single character constants. Multiple characters are Strings, and String constants are wrapped in double quotes. "foo" is a valid String constant. 'foo' is invalid syntax. Apr 11, 2011 at 15:28
6

try

IsAlternateRow = !IsAlternateRow;

(updated to show what this would look like in your code)

var IsAlternateRow = false;

if(/* -- insert equation here -- */) 
{
 // do something
} 
 else 
{
    IsAlternateRow = !IsAlternateRow;
}
6
  • 1
    it's a simple way to invert a boolean value. IsAlternateRow (interpret as false) ` = !IsAlternateRow `(interpret as !false, or true) This works from false to true and from true to false. Apr 11, 2011 at 15:20
  • @theJava: IsAlternateRow will === the opposite of what IsAlternateRow currently equals, in terms of Booleans.
    – Shaz
    Apr 11, 2011 at 15:20
  • I am just confused... can you place this code in my statement and show me... should i declare isAlternateRow somewhere
    – theJava
    Apr 11, 2011 at 15:22
  • I updated the answer to show how you would implement it in this scenario. Apr 11, 2011 at 15:39
  • should i place IsAlternateRow=='true' in my if condition
    – theJava
    Apr 12, 2011 at 6:40
4

return (IsAlternateRow.equals('true')) ? 'false' : 'true'

3

In general, I'd say that to answer questions like this, you should ask yourself,

Is this easy to read?

Shallow-nested if/else statements aren't terrible, but once you start nesting ad nauseum, you should probably refactor.

2

This is fine, but there are easier ways to do what you are doing:

IsAlternateRow = !IsAlternateRow
1

Yes you can. If you want to evaluate the same object/variable several time you could use a switch statement but in many case staggered if statement will do the job just as well.

1

To answer your question, yes, you can infinitely nest if/else statements. The code you supplied will compile no problem. Although given that you're talking about Java, I imagine unless that's pseudo code, it isn't going to provide the desired result.

1

Correct way to do it:

if() {

} else if (IsAlternateRow=='true') {
    IsAlternateRow = 'false';
}
 else
{
    IsAlternateRow = 'true';
}
1
  • using a string for boolean comparisons like this isn't the best way though. Apr 12, 2011 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.