-3

I took few help online to complete my task. And I found this code but I do not know the actual working as I have never used such syntax before in c++. The (?) Question mark and (:) colon.Can any one provide a simple general syntax code explaining same line?

x = (i-coins[j] >= 0)? table[i - coins[j]][j]: 0;
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182

2 Answers2

2

This means

if (i-coins[j] >= 0)
    x = table[i - coins[j]][j];
else
    x = 0;
meedz
  • 132
  • 13
0

This is called ternary operator, and it is used in place for a short if-else statement.

int factorial(int number) {
  if (number < 1) {
    return 1;
  } else {
    return number*(number-1);
  }
}

The above function can be summed up using a ternary operator:

int factorial(int number) {
  return (number < 1) ? 1 : number*(number-1);
}
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76