else methods ??

Hi. I have questions.

function abc()
{
  if(ab > varA)  // 1
   {...;}

  if(ab <= varA)  // 2
   {...;
     if(b)        // 3
      {......;}
     if(c)        // 4
      {.....;}
   }
  else
   {.....;
   }
}

Q1. in the above code, what is else’s counterpart? (1,2,3,4)

Q2. As you can see, variable ab is covered completely already before else statement.

Then, when the else statement work?

if ab > varA then 1 and the else statement will both trigger

else works if ab <= varA is NOT true, so it works when ab > varA.

And this case is already the case 1. The else has no reason to exist.

To clarify further, the else-statement cannot stand on its own. It needs to be immediately after an if statement. The reason being is that the else-statement gets executed only if the condition of the if-statement before it is false. In this case the only thing that can make “ab <= varA” false is “ab > varA” which is covered already in your abc function clearly making it redundant code.

Thanks all.

I also suspicious why that else exist.

But the code if from my temporary programming teacher, so that I confused.

I also can’t question to him because I learned by video.

:smile: