-1

I'm having a hard time understanding this statement here:

for( int i=0; i< out_length; i++){

     int num=i < length_a ? array_a[i] : 0;

...
...

what I googled:

expr1 ? expr2 : expr3

If expr1 evaluates to a non-zero value, expr2 is evaluated, otherwise expr3 is evaluated. The value of the expression as a whole is which ever of expr2 or expr3 is evaluated (this means the type of expr2 and expr3 must be the same).

but I'm still confused, it will be helpful if you can turn that statement into some if-else blocks, thanks for the help..

8 Answers 8

6

expr1 ? expr2 : expr3

Equivalent if else is:

if(expr1)
{
   //Evaluate expr2
}
else
{
   //Evaluate expr3
}

So your statement in the code evaluates as:

int num=i < length_a ? array_a[i] : 0;

means

if(i<length_a)
{
   num = array_a[i];
}
else
{
   num = 0;
}
2
  • 5
    The important difference is that the ternary operator is an expression, rather than a statement. So for example func(x?1:10) is valid syntax, whereas you could not do that with if statements (directly).
    – aruisdante
    Commented Mar 17, 2014 at 3:51
  • Yea thats true. I also clearly added that it means that and cannot always replace it.
    – SoulRayder
    Commented Mar 17, 2014 at 3:57
4

EXP1 ? exp2 : exp3

resolves to

if(exp1){  //if exp1==1
    exp2    //do this
  }
else{       //if exp2==0
    exp3    // do this
 }
3
int num=i < length_a ? array_a[i] : 0;

Equivalent to

int num;

if(i<length_a)
    num = array_a[i];
else
    num = 0;
3

The expression

int num=i < length_a ? array_a[i] : 0;

is equivalent to

if(i < length_a)
    num = array_a[i];
else
    num = 0;

In other words, if the first part evaluates to true, the whole expression is equal to the second part; whereas, if the first part evaluates to false, the whole expression is equal to the third part.

This is a ternary operator, as opposed to a binary operator.

More information on the ?: operator here: http://en.wikipedia.org/wiki/%3F:#C

2

Basically, the code you're wondering about is the same as:

int num;
if (i < length_a) {
    num = array_a[i];
} else {
    num = 0;
}
2
int num=i < length_a ? array_a[i] : 0;

This translates into

int num;

if (i < length_a)
{
  num = array_a[i];
}
else
{
  num = 0;
}

The first part is the conditional: i < length_a. If this evaluates to true, the second part is returned: array_a[i]. If the first part evaluates to false, the third and final part is returned: 0.

0

i hope your code is as follows

for( int i=0; i< out_length; i++){
 int num=i < length_a ? array_a[i] : 0;
}

and if you want in terms of if else... then here's the code.

 for( int i=0; i< out_length; i++)
{
   if(i<length_a)
  {
     int num = array_a[i];
  }
  else
  {
     int num = 0;
  }
}

i did code is so simple to make you understand the code easily, i hope this is what you are looking for.... and might help you to understand how to code in both the ways.

2
  • Not exactly, unless you intend to duplicate all code following the ternary operator. You should move the declaration of num to before the if statement so that its value can be used after the if statement.
    – pat
    Commented Mar 17, 2014 at 4:21
  • yes, i can do that and we can do lot of optimizations on the code like- removing '{' and '}' etc. but i converted given code literally in terms of if-else.... i did this way because i don't want to make code complex to read for a starter.
    – nikhil
    Commented Mar 17, 2014 at 4:51
0

int num = i < length_a ? array_a[i] : 0;

here the value of 'num' deals with the condition...i.e.,

when value of 'i' less than 'length_a' then, num=array_a[i]

otherwise, when 'i' greater than or equals to 'length_a' , num=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.