Herel Adrastel
Herel Adrastel

Reputation: 151

Go back to the parent if

I have this kind of code

if(a == 3) {
  if(b == 4) {
    // do somthing
  } else {
    // do an other thing  
  }
} else {
  // do the same other thing  
}

I wondered, when I am in the first else, how could I go to the second elsebecause it will execute the same code

Thank you

Upvotes: 0

Views: 83

Answers (1)

Eran
Eran

Reputation: 393936

You only want the // do something part to be executed when a==3 AND b==4, so you can combine them with an && operator.

This way you can merge the two conditions into one, and have a single else clause that performs the // do an other thing part :

if(a == 3 && b == 4) {
    // do something
} else {
    // do an other thing  
}

Upvotes: 5

Related Questions