JS question for If statement

What is the different between using IF statement alone and using else with it?

Using Else with if gives a alternative option in case if the condition fails. I’m not clear what do you want out your question

1 Like

In a flow chart, it looks like this:

3 Likes

If you only have a single ‘if’ statement w/o an accompanying ‘else’ statement, then the code within this lone ‘if’ statement will execute if and only if the statement evaluates as True. If, instead, you have it accompanied by an ‘else’ statement, then should the initial ‘if’ statement fail, it will automatically execute the code enclosed in the ‘else’ statement.

Scenario 1:
if (test) {
/* will execute if test evaluates as true */
}
/*code outside the if statement will execute whether or not the initial if statement evaluates as true*/

Scenario 2:
if (test 1) {
/* will execute test1 evaluates as true */
} else {
/* will execute if and only if test1 evaluates as false */
}

Scenario 3
if(test1) {
/* will execute test1 evaluates as true */
} else if (test2) {
/* will execute test2 evaluates as true AND test1 has evaluated as false */
} else {
/* will execute if and only if test1 and test2 evaluates as false */
}

Does that help at all?

3 Likes

What I mean, can I use IF statement without adding else to it or not (( as happened in the Logical challenge where if mentioned first time )) ?

Thank you for helping <3

Well, you seem to be the nearest one to understand what I mean. What I asked for what is the difference between scenario 1 and scenario 2 in your answer? I am not sure and correct me if I wrong - without else the code block out of the if parentses will excute even the if condition is true, but with ELSE it will excute only if the condition was wrong - is that right ?

Yes you can absolutely use If without an Else statement.

1 Like

Without an ‘else’, any code outside of an ‘if’ statement will execute as long as the ‘if’ doesn’t contain a ‘return’ etc.

yes, the code within an ‘else’ will execute if and only if the initial ‘if’ condition evaluates as false. but if there is a block of code outside of the ‘else’, it will also still execute.

if (test1) {
/* executes if and only if test1 evaluates as True */
} else {
/* executes if and only if test1 evaluates as False */
}
/* code here will still execute once the if/else has finished being evaluated */

1 Like

Well, that’s more clear now. Thank you for your help, dude :slight_smile:

I got it. Thank you man :slight_smile:

1 Like

nice. good luck!

one thing i do when i have trouble understanding how / why code is working a certain way is to do something like this:

if i wanted to understand what was happening w/ the if / else thing, I would just try to write test code for the different scenarios i’m curious about.

var str = '';

function() { 
  if (test1) {
    str = 'within if';
  } else {
    str = 'within else';
  }
  str = 'outside if';

  return str;
}

then call the function() to see how the output changes depending on whether or not test1 evaluates as True or False. you can play around w/ it and explore multiple scenarios this way. lmk whether or not that makes sense.

1 Like

Brilliant. I will remember this way for coming problems.

if (condition) {do something} else {do something else} is the same as if (condition) {do something}; if (!condition) {do something else}. In other words, it’s like “if not”, which works the same as in natural language:

If there’s bread, buy one loaf. If not, go to the next store.

var storeIdx = 0;
while (me.loaves === 0 && stores[storeIdx]) {
  if (stores[storeIdx].loaves >= 1) {
    me.money -= 1.00;
    stores[storeIdx].money += 1.00;
    stores[storeIdx].loaves -= 1;
    me.loaves += 1;
  } else {
    storeIdx++;
  }
}

If the door is open, go straight in. If not, knock first.

if (doorIsOpen) {
  goStraightIn();
} else {
  knockOnDoor();
}
1 Like

Thank you for helping :slight_smile: