Nesting if statements VS Elseif

Hi, I’m on lesson Introducing Else If Statements and I have a question.

wouldn’t this

if (condition) {
  return "condition is true";
} else if (2ndcondition) {
  return "second condition is true";
} 

be equal to this

if (condition) {
  return "condition is true";
 if (2ndcondition) {
  return "second condition is true";
}} 

Is there a difference?

No they are not the same.

In the first case, 2ndcondition will be evaluated if condition evaluates to false.

In the second case, because you nested the second if inside the first if, 2ndcondition will never be evaluated. To understand why, I recommend you read about what return statements do.

However, the first case is practically the same as:

if (condition) {
  return "condition is true";
}
if (2ndcondition) {
  return "second condition is true";
} 

They reason why they are the same also has to do with how return works.

1 Like

So elseif means or not and?

Now my confusion has skyrocketed.
Wouldn’t the second condition be evaluated if the first condition returned true? Isn’t that the point of nesting?

As I was saying, read about return (I’ve already given you the link), and trust me, understanding return isn’t hard but is of paramount importance to your later studying :wink:

1 Like

… Did you not see my question or are you just not answering it

I am hyper confused and telling me to read about return doesn’t help my confusion at all.

You really didn’t answer my second question. I know I’m not entitled to education, but please help me in a more effective way?

Please do understand that the reason people sometimes don’t give you an answer straight out is because, apart from helping you learn to code, we also want to help you establish your own methods of learning, and the latter is very important if you want to stay in the programming game for the long term.

But in this case I’ll give the answer because I don’t want you to get a bad impression about this forum or be discouraged from coding because of me:

If you click into the link I gave, you’ll see that the first line reads:

The return statement ends function execution and…

This means that, once a function executes a return statement, it’ll ignore whatever follows, including any further if statements.

So, in your nested if example:

if (condition) {
  return "condition is true";
 if (2ndcondition) {
  return "second condition is true";
}}
  • If condition evaluates to true, it’ll execute return "condition is true"; and ignore if (2ndcondition) {...}
  • If condition evaluates to false, because if (2ndcondition) {...} is inside if (condition) {...}, the former will not be executed.
1 Like

Hi

if (1stcondition) {
  return "condition is true";
 if (2ndcondition) {
  return "second condition is true";
}} 

Does this code work the way I think it does?
If 1stcondition is true, it evaluates whether 2ndcondition is true? But it does not evaluate the 2nd condition if the 1st condition does not resolve to true. Am I correct or am I wrong?

Good job, if I understand your question correctly (99.99% sure I do) that is correct!

No. That is not correct. The second condition is never run because the return statement immediately stops the function and prevents the next line from running.

3 Likes

Okay, forget the return statement. It is confusing you unnecessarily. Here is the first case:

if (condition) {
   console.log("Hello");
} else if (2ndcondition) {
   console.log("Good Bye");
}

If condition is true, just Hello is printed because else part is not executed. If condition is false and 2ndcondition is true, then Good Bye is printed. If both conditions are false, nothing is printed.
Here is the second case:

if (condition) {
   console.log("Hello");
   if (2ndcondition) {
      console.log("Good Bye");
   }
}

If the condition is true, Hello is printed. And then, if 2ndcondition is true, Good Bye is also printed. If condition is false, nothing is printed. The 2ndcondition won’t get tested because it is inside the first if statement.

Now, if you have

function test() {
   if (condition) {
    return "Hello";
   if (2ndcondition) {
      return "Good Bye";
   }
}

if the condition is true, return "Hello" is executed. When a return statement is executed, it jumps out immediately from the function. This means the if (2ndcondition) statement won’t get executed at all.

1 Like

ok but it otherwise works the way I thought? If the return statement wasn’t there?

This would never evaluate the 2ndcondition because if the first is true, it would return the value and ignore the following code. If you want it to check the second condition, you could change the order of the lines. However this still means that both the first AND second condition must be true.

if (1stcondition) {
   if (2ndcondition) {
    return "first AND second condition is true";
    }
 return "only first condition is true";
} 

You should really hear people recommendations, which point you to important matters in your code, you should understand better. Do not underestimate the return statement and look up the way it behaves.
Even if there was no return, the conditions would still run in different manner.

if (condition) {
  console.log ("condition is true");
} else if (2ndcondition) {
  console.log("second condition is true");
} 

In the above code, the second condition will be estimated only if the first condition returns false.

if (condition) {
  console.log("condition is true");
 if (2ndcondition) {
  console.log("second condition is true");
}} 

In this last code snippet, the second condition will run only if the first condition return true.
Im sure by now you could figure out this by other people replies

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.