Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're confusing your operator precedence and semantics.</p> <p>First of all, you want to say (speed >=50) &amp;&amp; (speed &lt;= 54)</p> <p>Second, in C the &amp;&amp; has a higher precedence, so your expression as it is means: If (year==1 AND speed>=50) OR (if my speed is lower than 54)...</p> <p>Third, you have an extra pair of () which suggests that you intended to prioritize things differently.</p> <p>Finally, I'll mention that code that has a bunch of similar looking if statements is fairly poorly written. A better way to implement it would be either with tables or data structures to represent the ranges, or at least a better organization.</p> <p>====</p> <p>Update: Now that you've posted the code, it really looks like you're working too hard on this... First of all, if or switch based on year (there should only be about 4 years). For each year, call a separate function to do the calculation, that will already be cleaner. For example:</p> <pre><code>if(year==1) return calculateFineForYear1(speed); if(year==2) return ... </code></pre> <p>(I don't know C#, but there should be some sort of switch statement)</p> <p>Now you can manage each year separately. Since your conditions are in an ascending order of speed, you could do something like:</p> <pre><code>if(speed&lt;50) return 0; if(speed&lt;55) return ...; if(speed&lt;60) return ...; if(speed&lt;65) return ... </code></pre> <p>It is still far from perfect, but it will already be a lot cleaner and manageable.</p> <p>Now, looking at your ifs, it looks like you don't even need that many ifs, because you are paying 50 + C*diff, so why not just calculate the diff (speed-50) and then just return the result of the calculation?</p> <p>For example:</p> <pre><code>Int total_above_limit = (speed – SPEED_LIMIT); Int increments_of_5_above_limit = (total_above_limit)/5 Return ( (INITIAL_FEE – 50) + COST_PER_5_OVER*increments_of_5_above_limit)) </code></pre> <p>(I don't know C#, I'm just guessing at the syntax)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload