-6
capacity = (cap > 0) ? cap : CAPACITY;

I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?

irish Senthil
  • 81
  • 1
  • 6
  • 4
    lookup ternary conditional expression – Eran Apr 23 '18 at 12:45
  • 2
    `if (cap > 0) { capacity = cap; } else { capacity = CAPACITY; }` – David Apr 23 '18 at 12:45
  • it's a ternary operator, and it looks like whoever wrote it was trying to accomplish whether or not to use a default value (CAPACITY). ternary is an "if else" construct – Mark Giaconia Apr 23 '18 at 12:49
  • 1
    nice naming of variables btw, `cap`, `capacity` and `CAPACITY` all mean the same thing. That is a sure way to confuse every reader. – luk2302 Apr 23 '18 at 12:58

1 Answers1

0

It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".

Fatmajk
  • 1,770
  • 1
  • 13
  • 22