title: How to write nested ?: statements
body: <p>I would like to shorten my code by the use of the <code>?:</code> (if-else-then) comparative operator instead of using the traditional <code>if{}else{}</code> blocks that inconveniently tend to take over the screen. I was never taught about this operator, and I would greatly appreciate any help regarding how to nest multiple comparisons within one line.</p>
  
  <p>This is the code that I would like to shorten:</p>
  
  <pre><code>if(y&lt;0)
      y=0;
  else
     if(y+h&gt;s.getHeight())
          y = s.getHeight()-h;
  </code></pre>
  
  <p>I managed to condense each condition to this (not nested):</p>
  
  <pre><code>y = (y&lt;0) ? 0 : y;
  y = (y+h&gt;s.getHeight()) ? s.getHeight()-h : y;
  </code></pre>
  
  <p>Is this the correct way to nest it?</p>
  
  <pre><code>y = (y&lt;0) ? 0 : ((y+h&gt;s.getHeight()) ? s.getHeight()-h : y);
  </code></pre>
  
  <p>Thank you!</p>
  
  <p>EDIT: I was given a link to another post pertaining to the ?: operator. <a href="https://stackoverflow.com/questions/29676600/the-three-way-operator">Link</a>. However, my question has to do with nesting instead of just a simple if statement. Therefore, my question is not a duplicate of that post.</p>