tags:

views:

164

answers:

6

How to return a value from a shortcut If-Else conditional statements ?

It works just fine when I used the following Block of Code ,

var val =0;

function slide() {

  if(val != 0 ) { 
      val++ ;
  }else {
     return ;
   }

 //rest of my  code
}

The Problem is, I want to return values from shortcut IF-else conditions as follow's

var val=0;

function slide() {

  val != 0 ? val++ : //here I want to return from function to stop execution of remaining  Block;

  //rest of my code
}
+1  A: 

You may be looking for something like this

(function() {
 return (val != 0)? val++ : false;
})();

However, I'm not sure what value you want to return in the event that val = 0.

Do you want to return the current val, or not return anything?

Jamie Wong
+4  A: 
return val != 0 ? val++ : otherval;

So here we have if Val is not equal to 0 return val + 1 else return otherval.

Take care

DRL
+4  A: 

No; the ternary operator (the if-else shortcut) operates on expressions; "return such-and-such" isn't an expression (it's a statement).

You can do

return (val != 0) ? val++ : false;

This will return false when val is not zero, as you want, but it will return val when val is 0, which is probably not what you want.

JacobM
+1  A: 
var val=0;

(function () {

 return (val != 0) ? val++ : false;


})();
Robusto
+3  A: 

If you want to have the identical behavior in an expression, you need to use the void operator:

function() {
    return val != 0 ? (void val++) : false;
}

This will return undefined if val != 0 is true and false otherwise just like your function.

Gumbo
+1... not sure if it answered the question but i learned something.
iandisme
+2  A: 

The "shortcut if/else", known as the ternary operator, doesn't work on statements, only expressions. val++ is an expression that yields the original value of val, but also increments val as a side effect. However, return false is a statement, so it's invalid in a ternary statement.

I don't understand exactly what you're getting at, but the most obvious thing to do here, as other answers have said, is:

return val != 0 ? val++ : false;

which is equivalent to:

if (val != 0) {
    val++;
    return val - 1;
} else {
    return false;
}

Doing what you're trying to do exactly is probably bad style. It looks to me like your goal is to write appealing code, so some potentially helpful tips follow.

For one, consider saying this instead:

if (val == 0)
    return false;

val++;

/* The rest of your function */

Second, the ?: operator is right-associative, which means you can chain it like so:

foo = number == 0 ? "zero"
    : number == 1 ? "one"
    : number == 2 ? "two"
    : null;

However, in this case, it may be more appealing to use switch/case:

switch (number) {
    case 0:
        foo = "zero";
        break;
    case 1:
        foo = "one";
        break;
    case 2:
        foo = "two";
        break;
    default:
        foo = null;
}

Lastly, the && (AND) and || (OR) operators short circuit, so if you say:

var result = doSomethingModest() || doSomethingInsane();

If doSomethingModest() returns a true value, doSomethingInsane() won't execute because the interpreter already knows this expression is going to be true, so it doesn't bother evaluating the next expression.

Joey Adams
Anurag
The *conditional operator* (it’s not *the* ternary operator but *a* ternary operator) is left-associative and not right-associative. And in your example the operators are not chained but nested: `foo = (number == 0 ? "zero" : (number == 1 ? "one" : (number == 2 ? "two" : null)))` (the conditional operator is simply also used in the false-expression).
Gumbo
@Gumbo: Actually, it is right-associative. `?:` acts like parenthesis on the inside (between the `?` and `:`), but acts like a right-associative operator on the outside. Removing the inside expressions, it is `number == 0 ?: (number == 1 ?: (number == 2 ?: null))`. If it were left-associative, it would be `((number == 0 ?: number == 1) ?: number == 2) ?: null` (which it's not). Anyway, thanks for pointing out the term *conditional operator*.
Joey Adams