<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Markbackcs</title>
    <description>The latest articles on DEV Community by Markbackcs (@markbackcs).</description>
    <link>https://dev.to/markbackcs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4204821%2Fa54ea63d-ea41-4183-9341-ff3e014545b0.png</url>
      <title>DEV Community: Markbackcs</title>
      <link>https://dev.to/markbackcs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markbackcs"/>
    <language>en</language>
    <item>
      <title>How to Count Nested Loops Correctly (and the 2 Mistakes Almost Everyone Makes)</title>
      <dc:creator>Markbackcs</dc:creator>
      <pubDate>Sat, 24 Oct 2026 15:09:14 +0000</pubDate>
      <link>https://dev.to/markbackcs/how-to-count-nested-loops-correctly-and-the-2-mistakes-almost-everyone-makes-e9o</link>
      <guid>https://dev.to/markbackcs/how-to-count-nested-loops-correctly-and-the-2-mistakes-almost-everyone-makes-e9o</guid>
      <description>&lt;p&gt;"Two loops, so it's n². Done."&lt;/p&gt;

&lt;p&gt;For the Big O, that's true. But if your course asks you to &lt;strong&gt;count the operations&lt;/strong&gt;, the assignments, the tests and so on, this is exactly where most students lose marks. Not because it's hard, but because two small things are easy to miss.&lt;/p&gt;

&lt;p&gt;So let's count one together, slowly, with pictures.&lt;/p&gt;

&lt;h2&gt;
  
  
  The algorithm
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuhluupcmbi9ic0uf2six.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuhluupcmbi9ic0uf2six.png" alt="A nested loop and the four things we'll count." width="800" height="347"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FOR i ← 1 to n DO
    FOR j ← 1 to n DO
        K
    END FOR
END FOR
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll count four things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assignments&lt;/strong&gt;: setting the loop variables, i ← 1 and j ← 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tests&lt;/strong&gt;: checking the loop condition, "is i still ≤ n?"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterations&lt;/strong&gt;: moving the loop variable on, i ← i + 1 and j ← j + 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution&lt;/strong&gt;: how many times K actually runs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  First, the big picture
&lt;/h2&gt;

&lt;p&gt;Before counting anything, picture what's happening.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvuq9duxfo564fpekt3to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvuq9duxfo564fpekt3to.png" alt="Each row is one pass of the outer loop. Inside each row, the inner loop runs from 1 to n." width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time the outer loop runs once, the inner loop runs &lt;strong&gt;all the way through&lt;/strong&gt;. So with n = 3, you get 3 rows of 3, and K runs 9 times.&lt;/p&gt;

&lt;p&gt;Keep this picture in mind. It explains every count below: &lt;strong&gt;anything inside the outer loop happens once per outer pass&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one rule to remember
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk467ar0z2uw0apjl1fkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk467ar0z2uw0apjl1fkw.png" alt="Three checks pass, then one more check fails and ends the loop." width="799" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A loop from 1 to n doesn't check its condition n times. It checks it &lt;strong&gt;n + 1 times&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With n = 3, the loop checks i = 1, 2 and 3, and all three pass. Then it checks i = 4. That check fails, and that's what ends the loop. But it still happened, so it still counts.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Assignments
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7e72fye328qiyxhue2vw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7e72fye328qiyxhue2vw.png" alt="i is set once. j is set again at the start of every outer pass.&lt;br&gt;
" width="799" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The outer loop sets &lt;strong&gt;i ← 1 once&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The inner loop sets &lt;strong&gt;j ← 1 every time it starts&lt;/strong&gt;, and it starts once per outer pass, so &lt;strong&gt;n times&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Assignments = 1 + n&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Tests
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F36a0i25gc27m3q9v56wi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F36a0i25gc27m3q9v56wi.png" alt="The outer loop checks n + 1 times. On each of its n passes, the inner loop checks n + 1 times too.&lt;br&gt;
" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The outer loop checks its condition &lt;strong&gt;n + 1&lt;/strong&gt; times.&lt;/li&gt;
&lt;li&gt;On each of the n outer passes, the inner loop checks &lt;strong&gt;n + 1&lt;/strong&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the inner loop's tests are &lt;strong&gt;n(n + 1)&lt;/strong&gt;, not n + 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tests = (n + 1) + n(n + 1) = (n + 1)²&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Notice the last row of the picture: when the outer check fails (i = 4), the outer loop stops, and the inner loop never starts. That's why the inner checks are multiplied by n, not by n + 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Iterations
&lt;/h2&gt;

&lt;p&gt;Same idea, but now we count how many times each loop moves forward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;i&lt;/strong&gt; moves on &lt;strong&gt;n&lt;/strong&gt; times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;j&lt;/strong&gt; moves on n times per outer pass, so &lt;strong&gt;n · n = n²&lt;/strong&gt; times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Iterations = n + n²&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Execution
&lt;/h2&gt;

&lt;p&gt;Go back to the grid. K runs once in every cell:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution = n · n = n²&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your course uses summations, this is written as a double sum: the outer sum over i, the inner sum over j.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fev4gobdqz5dpzpm4xsyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fev4gobdqz5dpzpm4xsyw.png" alt="Add the four counts and simplify." width="799" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;T(n) = (1 + n) + (n + 1)² + (n + n²) + n²&lt;/p&gt;

&lt;p&gt;Expand (n + 1)² = n² + 2n + 1, collect the terms, and you get:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;T(n) = 3n² + 4n + 2 ∈ O(n²)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Always check with a small n
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4o8abd5wd10pm7b1bnfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4o8abd5wd10pm7b1bnfc.png" alt="Count by hand for n = 2, then compare with the formula." width="799" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This takes one minute and catches almost every mistake. Pick n = 2 and count each part by hand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assignments: 1 + 2 = &lt;strong&gt;3&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Tests: (2 + 1)² = &lt;strong&gt;9&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Iterations: 2 + 4 = &lt;strong&gt;6&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution: 2² = &lt;strong&gt;4&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's 22 in total, and the formula gives 3(4) + 8 + 2 = &lt;strong&gt;22&lt;/strong&gt;. ✓&lt;/p&gt;

&lt;h2&gt;
  
  
  The 2 mistakes almost everyone makes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: adding the inner loop instead of multiplying it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx86b1z2ywt2vl7yk4cbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx86b1z2ywt2vl7yk4cbv.png" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The inner loop isn't something that happens once, next to the outer loop. It happens &lt;strong&gt;inside&lt;/strong&gt; it, on every pass. So its counts get multiplied by the number of outer passes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 2: forgetting the check that fails.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fywwdcy024co3qgqutpg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fywwdcy024co3qgqutpg0.png" width="799" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every loop has one extra check at the end, the one that fails and stops the loop. Leave it out, and every count that involves tests is off.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: when the inner loop starts at i
&lt;/h2&gt;

&lt;p&gt;What changes if the inner loop is &lt;code&gt;FOR j ← i to n&lt;/code&gt; instead?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fprsozwrhvw6545siaumg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fprsozwrhvw6545siaumg.png" alt="Now each row is one shorter than the one before.&lt;br&gt;
" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the inner loop runs n times, then n − 1, then n − 2, all the way down to 1. Instead of a full square, you get a triangle:&lt;/p&gt;

&lt;p&gt;n + (n − 1) + … + 1 = n(n + 1) / 2&lt;/p&gt;

&lt;p&gt;It's about half the work, but it still grows like n², so it's still &lt;strong&gt;O(n²)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try these yourself
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Count the tests for &lt;code&gt;FOR i ← 1 to n&lt;/code&gt; with &lt;code&gt;FOR j ← 1 to 2n&lt;/code&gt; inside it.&lt;/li&gt;
&lt;li&gt;How many times does K run if the inner loop is &lt;code&gt;FOR j ← 1 to i&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Three nested loops, each from 1 to n: how many times does the innermost statement run?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Answers&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Outer: n + 1. Inner: 2n + 1 per pass, over n passes. Total: (n + 1) + n(2n + 1) = 2n² + 2n + 1.&lt;/li&gt;
&lt;li&gt;1 + 2 + … + n = n(n + 1) / 2.&lt;/li&gt;
&lt;li&gt;n · n · n = n³.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;Two ideas get you through almost every nested loop question:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Anything inside the outer loop happens once per outer pass&lt;/strong&gt;, so multiply.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A loop from 1 to n tests n + 1 times&lt;/strong&gt;, because the check that fails still counts.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Write the four headings, Assignments, Tests, Iterations and Execution, before you start counting, and check your answer with n = 2 before you move on.&lt;/p&gt;

&lt;p&gt;I lost marks on exactly this kind of counting myself, which is a big part of why I started making animated lessons that build these counts step by step, one line at a time. If you'd like to watch this one in motion, the lesson on analysing nested loops is free to watch, no account needed: &lt;a href="https://markbackcs.com/courses/algorithms/time-complexity-analysis/analysing-nested-loops" rel="noopener noreferrer"&gt;Analysing Nested Loops on Markback&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if a loop question has you stuck, drop it in the comments. I'm happy to count it through with you.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
