title: Calculating the frequency of an if statements in a loop and the total Big Oh
body: <p>I have an example that I found online, and I wanted to check its frequency and the total Big Oh. However, I'm having trouble calculating the frequency of an if-else statement inside a loop, and I can't seem to find the answer online.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>1 int i, j, sum = 0;               // Frequency of 1
  2 for (i = 0; i &lt; n; i++) {        // Frequency of n + 1
  3     if (i != 0)                  // Frequency of n
  4         sum += i;                // Frequency of n-1
  5     else for (j = 0; j &lt; n; j++) // Frequency of n + 1
  6         sum += j;                // Frequency of n
  7 }                                // O(n)
  </code></pre>
  
  <p>I know when calculating the Big Oh, I should be looking at the worst case, but isn't at worst the else will run 1 time only? if so is my reasoning and solution correct? or should I assume that for example at line 5 will run (n (n + 1)) times? which will turn this into O(n^2)</p>
  
  <p>This has been bugging me for days, and I've searched everywhere for the correct way to go about this, and I give up trying to figure it out.</p>