<?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: Jos</title>
    <description>The latest articles on DEV Community by Jos (@jos512).</description>
    <link>https://dev.to/jos512</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87391%2F898118a2-83ef-4b43-b7d6-04fd7a042567.png</url>
      <title>DEV Community: Jos</title>
      <link>https://dev.to/jos512</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jos512"/>
    <language>en</language>
    <item>
      <title>8 Common Causes of Infinite C# Loops</title>
      <dc:creator>Jos</dc:creator>
      <pubDate>Mon, 16 Dec 2019 17:56:39 +0000</pubDate>
      <link>https://dev.to/jos512/8-common-causes-of-infinite-c-loops-4ab</link>
      <guid>https://dev.to/jos512/8-common-causes-of-infinite-c-loops-4ab</guid>
      <description>&lt;p&gt;&lt;em&gt;This article appeared earlier on &lt;a href="https://kodify.net/csharp-programming-articles/"&gt;my website&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An infinite loop makes our C# program stuck, and can even cause an application crash. Luckily, infinite loops are often easy to solve. This article shows 8 common causes and how to fix them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix infinite loops: 8 causes
&lt;/h2&gt;

&lt;p&gt;An &lt;em&gt;&lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;infinite loop&lt;/a&gt;&lt;/em&gt; is a loop that keeps running 'forever'. These loops don't exit like regular &lt;a href="https://kodify.net/csharp/loop/overview-loops/"&gt;C# loops&lt;/a&gt; do, but instead use up computer resources and freeze our application.&lt;/p&gt;

&lt;p&gt;Most infinite loops are caused by either a coding or logic mistake. Some faults are pretty easy to spot, while others are more subtle.&lt;/p&gt;

&lt;p&gt;Here are several reasons why your C# code ran into &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;an infinite loop&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Exit condition that can never be met.&lt;/li&gt;
&lt;li&gt;Condition that makes the loop to start over again and again.&lt;/li&gt;
&lt;li&gt;Loop variable that's reset to a new value inside the loop.&lt;/li&gt;
&lt;li&gt;Loop without an exit condition.&lt;/li&gt;
&lt;li&gt;Loop that doesn't change the loop variable (like a missing increment or decrement statement).&lt;/li&gt;
&lt;li&gt;Mistakingly use the increment (&lt;code&gt;++&lt;/code&gt;) rather than decrement (&lt;code&gt;--&lt;/code&gt;) operator (or vice versa).&lt;/li&gt;
&lt;li&gt;Incorrect logical comparison in the loop's condition (for instance, &lt;code&gt;&amp;lt;&lt;/code&gt; rather than &lt;code&gt;&amp;gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;continue&lt;/code&gt; statement in the wrong place so that it jumps over code that changes the loop variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you run into an infinite loop, chances are your code has one of the above problem. Let's take a closer look at each and see what the fix is.&lt;/p&gt;

&lt;h3&gt;
  
  
  An exit condition that's never reached
&lt;/h3&gt;

&lt;p&gt;One thing that causes an infinite loop is when our code has an exit condition, but that condition can't be reached. This is a bit tricky, because our loop &lt;em&gt;does&lt;/em&gt; have all the required components. Yet when it runs, it doesn't stop.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;275&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This intentional infinite &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; prints the value of the &lt;code&gt;i&lt;/code&gt; variable during each loop cycle. Then it increases that variable with one (&lt;code&gt;i++&lt;/code&gt;). When that variable is above 275, &lt;a href="https://kodify.net/csharp/loop/break/"&gt;the &lt;code&gt;break&lt;/code&gt; statement&lt;/a&gt; ends the loop.&lt;/p&gt;

&lt;p&gt;Or, at least, that's the idea. In practice this loop remains stuck. The cause for this is the &lt;code&gt;byte&lt;/code&gt; loop variable. A &lt;code&gt;byte&lt;/code&gt; variable can hold the values 0 through 255. When its value reaches that upper level, the variable's value wraps around and starts again from zero. That makes it impossible for &lt;code&gt;i&lt;/code&gt; to become greater than 275.&lt;/p&gt;

&lt;p&gt;The solution is to use a loop variable of a different type, like a regular integer (&lt;code&gt;int&lt;/code&gt;) variable:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fixed: loop variable now an integer&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;275&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;A similar issue can happen when our loop variable is a floating-point value. Since float values cannot be represented exactly, a loop test like &lt;code&gt;while (i != 1.1)&lt;/code&gt; fails when floating-point imprecision gives our loop variable a value like &lt;code&gt;1.100000000000001&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A condition that makes the loop start over and over again
&lt;/h3&gt;

&lt;p&gt;An infinite loop also happens when our loop condition doesn't terminate the loop, but rather make it start over again. This can be difficult to spot: usually the code compiles fine and there's no obvious thing missing. Yet one misspelled or missing character can cause this issue.&lt;/p&gt;

&lt;p&gt;Here's an example of that mistake:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Loop 1,000 times at most&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;125&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; continues as long as the &lt;code&gt;i&lt;/code&gt; variable is under &lt;code&gt;1000&lt;/code&gt;. The loop has &lt;a href="https://kodify.net/csharp/if-else/if-statement/"&gt;an if statement&lt;/a&gt; that checks if that variable is above 125. When it is, &lt;a href="https://kodify.net/csharp/loop/break/"&gt;the &lt;code&gt;break&lt;/code&gt; statement&lt;/a&gt; ends the loop early.&lt;/p&gt;

&lt;p&gt;That's the idea at least. But the loop's body doesn't change the &lt;code&gt;i&lt;/code&gt; variable. It looks like this should be happening in the loop condition. But there we have a strange thing: the loop condition assigns &lt;code&gt;i&lt;/code&gt; a value of &lt;code&gt;1&lt;/code&gt; each time. Then it looks if that assignment returns a value under &lt;code&gt;1000&lt;/code&gt; -- which it of course always is.&lt;/p&gt;

&lt;p&gt;What the loop should do is increment &lt;code&gt;i&lt;/code&gt; with one with each loop cycle. To implement that fix we add one character, and change &lt;code&gt;i = 1&lt;/code&gt; to &lt;code&gt;i += 1&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Loop 1,000 times at most&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;125&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Change the loop variable to a new value inside the loop
&lt;/h3&gt;

&lt;p&gt;Usually a variable controls for how long a loop should run. But if we accidentally reset that variable inside the loop, our loop doesn't get closer to its exit condition no matter how many cycles it runs.&lt;/p&gt;

&lt;p&gt;Here's &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;an infinite loop&lt;/a&gt; example of that scenario:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; continues for as long as the &lt;code&gt;i&lt;/code&gt; variable is under 10. At the end of the loop's code, we increment (&lt;code&gt;++&lt;/code&gt;) that variable with one. That should increase the variable's value all the way up to 10.&lt;/p&gt;

&lt;p&gt;But inside the loop we also set the &lt;code&gt;i&lt;/code&gt; variable to &lt;code&gt;3&lt;/code&gt; each time. That always keeps the loop variable under 10, and the loop never ends. Since that statement doesn't seem to serve a purpose, we simply remove it to fix the problem:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On a related note, it usually is a good idea to make your loop variable's name different from other variables. That way you don't mistake the loop variable for another local variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  A loop without an exit condition
&lt;/h3&gt;

&lt;p&gt;Another thing that causes a stuck infinite loop is when we don't have an exit condition. This cause is easy to spot, but unfortunately also a mistake that's quickly to make.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; is an &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;intentional infinite loop&lt;/a&gt;. That's fine, but the loop doesn't have an exit condition. So the loop keeps running forever.&lt;/p&gt;

&lt;p&gt;Luckily, to fix this code we just have to add an exit condition. One option is to make one inside the &lt;code&gt;while&lt;/code&gt; condition:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Fixed: add an exit condition to the loop&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or we keep the &lt;code&gt;true&lt;/code&gt; loop condition and make the loop stop with &lt;a href="https://kodify.net/csharp/loop/break/"&gt;the &lt;code&gt;break&lt;/code&gt; statement&lt;/a&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;

    &lt;span class="c1"&gt;// Fixed: terminate the loop with 'break'&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  A loop that doesn't change the loop variable's value
&lt;/h3&gt;

&lt;p&gt;Another thing that causes an &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;infinite C# loop&lt;/a&gt; is when we don't change the loop variable. This mistake is easy to make; a quick interruption and this loop component is overlooked. Luckily, the fix is easy and straightforward.&lt;/p&gt;

&lt;p&gt;Here's how the issue can look:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; continues as long as the &lt;code&gt;i&lt;/code&gt; variable is under 10. However, we don't change that variable's value. And so the loop doesn't progress.&lt;/p&gt;

&lt;p&gt;To fix this infinite loop we add a statement to the loop's code that changes that variable. Here's one option:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Fixed: increase the loop variable&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Mixing up the increment (&lt;code&gt;++&lt;/code&gt;) or decrement (&lt;code&gt;--&lt;/code&gt;) operators
&lt;/h3&gt;

&lt;p&gt;Sometimes our loop &lt;em&gt;does&lt;/em&gt; change the loop variable, but in the wrong direction. One situation in which that can happen is when we accidentally swap the increment operator (&lt;code&gt;++&lt;/code&gt;) for the decrement operator (&lt;code&gt;--&lt;/code&gt;), or vice versa. When we do, it takes a very long time before the loop variable reaches its 'exit value'.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Accidentally decrease, rather than increase, the loop variable&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;--)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/for-loop/"&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt; has a loop variable that starts at &lt;code&gt;0&lt;/code&gt;. The loop itself goes on as long as that variable is under &lt;code&gt;10&lt;/code&gt;. We don't, however, increase that variable after each loop cycle. Instead the &lt;code&gt;i--&lt;/code&gt; expression &lt;em&gt;decreases&lt;/em&gt; the variable.&lt;/p&gt;

&lt;p&gt;One solution is to increase that variable after each loop cycle:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fixed: increase 'i' up to 10&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or, if we meant to make a reversed loop, change the &lt;code&gt;for&lt;/code&gt; loop header to this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Loop from 10 to 1&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;--)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Wrong logical comparison in the loop's condition
&lt;/h3&gt;

&lt;p&gt;Another thing that creates &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;an infinite loop&lt;/a&gt; is when our loop condition performs the wrong logical comparison. We might, for instance, use &lt;code&gt;&amp;gt;&lt;/code&gt; instead of &lt;code&gt;&amp;lt;&lt;/code&gt;. Or make a comparison with the wrong variable.&lt;/p&gt;

&lt;p&gt;An example of that latter is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; tests the &lt;code&gt;j&lt;/code&gt; variable, while the loop itself works with the &lt;code&gt;i&lt;/code&gt; variable. That latter variable is what &lt;code&gt;Console.WriteLine()&lt;/code&gt; and the increment (&lt;code&gt;++&lt;/code&gt;) statement use.&lt;/p&gt;

&lt;p&gt;So judging from the loop's code, we probably meant to test the &lt;code&gt;i&lt;/code&gt; variable. So to fix it we do:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Fixed: loop based on the 'i' variable&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  The continue statement at the wrong place
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://kodify.net/csharp/loop/continue/"&gt;C#'s &lt;code&gt;continue&lt;/code&gt; statement&lt;/a&gt; makes an early start of the next loop cycle. When our program executes &lt;code&gt;continue&lt;/code&gt;, it jumps over the remaining code in the loop and revisits the loop's header. &lt;/p&gt;

&lt;p&gt;A possible risk is that we accidentally jump over code that should run with &lt;em&gt;every&lt;/em&gt; loop cycle. An example of that issue is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Skip all even values&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://kodify.net/csharp/if-else/if-statement/"&gt;if statement&lt;/a&gt; in this &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; checks if the &lt;code&gt;i&lt;/code&gt; variable has an even value. When it has, &lt;a href="https://kodify.net/csharp/loop/continue/"&gt;the &lt;code&gt;continue&lt;/code&gt; statement&lt;/a&gt; gives the loop an early start on the next loop cycle.&lt;/p&gt;

&lt;p&gt;That, however, also jumps over the statement that changes the loop variable (&lt;code&gt;i++&lt;/code&gt;). So whenever the if statement's condition tests &lt;code&gt;true&lt;/code&gt;, the &lt;code&gt;i&lt;/code&gt; variable doesn't change anymore and we have &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;an infinite loop&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The solution is to move &lt;code&gt;continue&lt;/code&gt; after the code that changes the loop variable. Or, alternatively, increase that variable before &lt;code&gt;continue&lt;/code&gt; runs. For example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fixed: increase variable before 'continue'&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;

    &lt;span class="c1"&gt;// Skip all even values&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"i = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This ends our discussion of common causes of &lt;a href="https://kodify.net/csharp/loop/infinite-loop/"&gt;infinite loops&lt;/a&gt;. Thanks for reading and I hope you learned something new.&lt;/p&gt;

&lt;p&gt;If you want to learn more, see all my &lt;a href="https://kodify.net/csharp-programming-articles/"&gt;C# programming articles&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy holidays!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>loop</category>
      <category>infiniteloop</category>
    </item>
    <item>
      <title>The four different loops of C#</title>
      <dc:creator>Jos</dc:creator>
      <pubDate>Mon, 02 Sep 2019 15:09:01 +0000</pubDate>
      <link>https://dev.to/jos512/the-four-different-loops-of-c-34m3</link>
      <guid>https://dev.to/jos512/the-four-different-loops-of-c-34m3</guid>
      <description>&lt;p&gt;&lt;em&gt;This article appeared earlier on &lt;a href="https://kodify.net/csharp-programming-articles/"&gt;my website&lt;/a&gt;. It's also my first article here on Dev.to! :-)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To repeat code in C# we can use four different loops. But what loops are there and how do we use them? Let's find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looping in C# programs: four different options
&lt;/h2&gt;

&lt;p&gt;A loop repeats the same code several times. They make calculations and processing elements in a collection possible. Together with if statements they're an essential feature to control how our program executes.&lt;/p&gt;

&lt;p&gt;Not every situation requires the same loop. And so C# has several loop options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;for&lt;/code&gt; loop, which counts from one value to another.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;foreach&lt;/code&gt; loop, that easily iterates over all elements in a collection.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;while&lt;/code&gt; loop, which goes on as long as some condition is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;And the &lt;code&gt;do-while&lt;/code&gt; loop, which always executes at least once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a closer look at these loop types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make a counting loop: C#'s &lt;code&gt;for&lt;/code&gt;loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kodify.net/csharp/loop/for-loop/"&gt;The &lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt; repeats code up to a certain number of times. That behaviour is possible because this loop counts from one value to the next. This way we perform calculations and loop over a list or array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop is also a compact loop. It combines three common loop actions together: declare and initialise the loop variable, check the loop condition before each loop cycle, and update the loop variable after each iteration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick example: the &lt;code&gt;for&lt;/code&gt;loop in C
&lt;/h3&gt;

&lt;p&gt;Here's how C#'s &lt;code&gt;for&lt;/code&gt; loop looks:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The header of this &lt;code&gt;for&lt;/code&gt; loop has three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we declare and initialise the &lt;code&gt;i&lt;/code&gt; loop variable to a value of &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The loop condition checks if the &lt;code&gt;i&lt;/code&gt; variable is under (&lt;code&gt;&amp;lt;&lt;/code&gt;) 5. When it is, code inside the loop executes. When the condition tests &lt;code&gt;false&lt;/code&gt;, the loop ends.&lt;/li&gt;
&lt;li&gt;The last part updates the &lt;code&gt;i&lt;/code&gt; loop variable with one after each loop cycle (&lt;code&gt;i++&lt;/code&gt;). This way we iterate from one value to the next.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See &lt;a href="https://kodify.net/csharp/loop/for-loop/"&gt;C#'s &lt;code&gt;for&lt;/code&gt; loop explained&lt;/a&gt; for much more. There's also an article about &lt;a href="https://kodify.net/csharp/loop/alternative-for/"&gt;alternative &lt;code&gt;for&lt;/code&gt; loops in C#&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use the &lt;code&gt;for&lt;/code&gt;loop?
&lt;/h3&gt;

&lt;p&gt;These are the situations in which we can use a &lt;code&gt;for&lt;/code&gt; loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When code needs to execute a specific number of times. Since &lt;code&gt;for&lt;/code&gt; automatically creates and updates the loop variable, it's easier to work with than other loops in those situations.&lt;/li&gt;
&lt;li&gt;When code inside the loop needs a counting variable. Other C# loops can also use a counting variable, but the &lt;code&gt;for&lt;/code&gt; loop manages one for us.&lt;/li&gt;
&lt;li&gt;When code should not execute more than a certain number of times. In those cases &lt;code&gt;for&lt;/code&gt; can easily count towards those loop limits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Strengths and weaknesses of C#'s &lt;code&gt;for&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;These are the strengths of C#'s &lt;code&gt;for&lt;/code&gt; loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;for&lt;/code&gt; loop makes it easy to modify a collection or examine adjacent (the next or previous) elements.&lt;/li&gt;
&lt;li&gt;Once we make the &lt;code&gt;for&lt;/code&gt; loop header, we can focus on code inside the loop. There's no need to manage the loop variable inside the loop.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;for&lt;/code&gt; loop combines making a loop variable, updating that loop variable, and evaluating the loop's condition into a single line. This makes for a compact loop.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;for&lt;/code&gt; loop header &lt;em&gt;always&lt;/em&gt; changes the loop variable after each loop cycle. There's no risk that we accidentally jump over the code that changes that variable with &lt;a href="https://kodify.net/csharp/loop/continue/"&gt;the &lt;code&gt;continue&lt;/code&gt; statement&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop also has some weaknesses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If all we need is to loop over a collection, the &lt;code&gt;for&lt;/code&gt; loop is more complex than needed. &lt;a href="https://kodify.net/csharp/loop/foreach-loop/"&gt;A &lt;code&gt;foreach&lt;/code&gt; loop&lt;/a&gt; is then easier to work with.&lt;/li&gt;
&lt;li&gt;Not every expression can be used in the &lt;code&gt;for&lt;/code&gt; loop header to change the loop variable. If we need to change that variable in complex ways, the code for that has to be placed inside the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Easily loop through a collection: C#'s &lt;code&gt;foreach&lt;/code&gt;loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kodify.net/csharp/loop/foreach-loop/"&gt;The &lt;code&gt;foreach&lt;/code&gt; loop&lt;/a&gt; elegantly loops through all items in a collection. The key feature of &lt;code&gt;foreach&lt;/code&gt; is its loop variable. That variable is set to an element from the collection we loop over during each loop cycle.&lt;/p&gt;

&lt;p&gt;Inside the loop we use that variable to work with the element's value. Then after each loop cycle, C# automatically sets that variable to the value of the next element in the collection. This makes &lt;code&gt;foreach&lt;/code&gt; a lot easier to work with than C#'s other loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick example: the &lt;code&gt;foreach&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;Here's how C#'s &lt;code&gt;foreach&lt;/code&gt; loop looks:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The header of each &lt;code&gt;foreach&lt;/code&gt; loop has three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we define the type of loop variable. The loop above uses &lt;code&gt;int&lt;/code&gt; because the array we loop over contains integer values.&lt;/li&gt;
&lt;li&gt;Then we name the local loop variable. We use &lt;code&gt;value&lt;/code&gt; in the above loop. In the loop's body, that variable refers to the element we currently loop over.&lt;/li&gt;
&lt;li&gt;With the &lt;code&gt;in&lt;/code&gt; keyword we specify the collection of values to loop over. We use &lt;code&gt;values&lt;/code&gt; here, which is the integer array that's defined just above the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See &lt;a href="https://kodify.net/csharp/loop/foreach-loop/"&gt;C#'s &lt;code&gt;foreach&lt;/code&gt; loop explained&lt;/a&gt; for more details.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use the &lt;code&gt;foreach&lt;/code&gt;loop?
&lt;/h3&gt;

&lt;p&gt;We use C#'s &lt;code&gt;foreach&lt;/code&gt; loop to work with each value from a collection. For that purpose &lt;code&gt;foreach&lt;/code&gt; is a more convenient alternative to the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;foreach&lt;/code&gt; cannot change the value it loops over. Or only iterate through part of the collection. For those two scenarios we still use the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strengths and weaknesses of the &lt;code&gt;foreach&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;Here are some advantages of the &lt;code&gt;foreach&lt;/code&gt; loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's an easy loop to write; C# automatically manages the loop variable and sets it to an element during each loop cycle. That makes the loop easier to write but also reduces possible mistakes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;foreach&lt;/code&gt; automatically goes through all elements in a collection. There's no need to manage the loop's start and end index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some &lt;code&gt;foreach&lt;/code&gt; disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because the &lt;code&gt;foreach&lt;/code&gt; loop works with a copy of the element we loop over (rather than the actual element), we cannot make changes to the collection we loop over. To make those changes we need to use a &lt;code&gt;for&lt;/code&gt; loop, &lt;code&gt;while&lt;/code&gt; loop, or &lt;code&gt;do-while&lt;/code&gt; loop instead.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;foreach&lt;/code&gt; loop always goes through an entire collection. To process only a part, or skip certain elements, we can make a new collection or use the &lt;code&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;foreach&lt;/code&gt; loop always loops from index &lt;code&gt;0&lt;/code&gt; to index &lt;code&gt;Length - 1&lt;/code&gt; (or &lt;code&gt;Count - 1&lt;/code&gt;). If the loop should iterate backward, it's easier to use a &lt;code&gt;for&lt;/code&gt; loop instead.&lt;/li&gt;
&lt;li&gt;When our code also has to work with the element's index (besides its value), then the &lt;code&gt;for&lt;/code&gt; loop is an easier choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Loop as long as something is &lt;code&gt;true&lt;/code&gt;: the &lt;code&gt;while&lt;/code&gt;loop
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;The &lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; executes code for as long as some Boolean condition tests &lt;code&gt;true&lt;/code&gt;. When that condition becomes &lt;code&gt;false&lt;/code&gt;, the loop ends.&lt;/p&gt;

&lt;p&gt;The strength of &lt;code&gt;while&lt;/code&gt; is that we don't need to know in advance how many loop cycles we need. Instead the loop simply goes on for as long as its condition tests &lt;code&gt;true&lt;/code&gt;. We do need some code inside the loop to make that condition &lt;code&gt;false&lt;/code&gt; at some point; otherwise, the loop keeps running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick example: C#'s &lt;code&gt;while&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;Here's how C#'s &lt;code&gt;while&lt;/code&gt; loop looks like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int i = 0;
    while (i &amp;lt; 5)
    {
        Console.Write(i + " ");
        i++;
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Before the loop we first make our &lt;code&gt;i&lt;/code&gt; integer variable with a value of &lt;code&gt;0&lt;/code&gt;. Then the &lt;code&gt;while&lt;/code&gt; loop begins. Its condition checks if that variable is under (&lt;code&gt;&amp;lt;&lt;/code&gt;) 5. When it is, code inside the loop executes.&lt;/p&gt;

&lt;p&gt;That code also increases the value of &lt;code&gt;i&lt;/code&gt; with 1 (&lt;code&gt;i++&lt;/code&gt;). That makes the loop's condition (&lt;code&gt;i &amp;lt; 5&lt;/code&gt;) &lt;code&gt;false&lt;/code&gt; after 5 loop cycles. (The alternative is that we get stuck with an infinite loop that doesn't end.)&lt;/p&gt;

&lt;p&gt;See &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;C#'s &lt;code&gt;while&lt;/code&gt; loop explained&lt;/a&gt; to learn more.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use the &lt;code&gt;while&lt;/code&gt;loop?
&lt;/h3&gt;

&lt;p&gt;We use the &lt;code&gt;while&lt;/code&gt; loop when we have to repeat code several times, but don't know how many loop cycles we need. This for instance happens when we process user input or crawl a website. In those situations we might need 10 loop cycles or 450 iterations. With a &lt;code&gt;while&lt;/code&gt; loop we simply continue until some condition turns up &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another use case are complex calculations that don't involve the basic counting that the &lt;code&gt;for&lt;/code&gt; loop provides.&lt;/p&gt;

&lt;p&gt;Sometimes we also use the &lt;code&gt;while&lt;/code&gt; loop to deliberately make an infinite loop. That way we can have our program 'wait' on certain things, like incoming data. In those cases &lt;code&gt;while&lt;/code&gt; keeps our program active and ready.&lt;/p&gt;

&lt;p&gt;If we have to loop over values of a collection, then it's often easier to use the &lt;code&gt;foreach&lt;/code&gt; loop. And looping from one numerical value to another is easier with the &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strengths and weaknesses of the &lt;code&gt;while&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;These things the &lt;code&gt;while&lt;/code&gt; loop does well:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;while&lt;/code&gt; loop is an intuitive loop that's easy to read and understand. It's also a common loop, so many programmers understand your code when you use &lt;code&gt;while&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;while&lt;/code&gt; loop continues until its condition tests &lt;code&gt;false&lt;/code&gt;. That helps when we don't know the number of loop cycles in advance.&lt;/li&gt;
&lt;li&gt;We can often update the loop variable &lt;em&gt;inside&lt;/em&gt; the &lt;code&gt;while&lt;/code&gt; loop header for details). That makes the loop's body more compact.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some weaknesses that the &lt;code&gt;while&lt;/code&gt; loop has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;while&lt;/code&gt; loop often needs a statement inside its loop body that changes the loop condition. That means extra code. Plus code that we can easily skip over by accident with &lt;a href="https://kodify.net/csharp/loop/continue/"&gt;the &lt;code&gt;continue&lt;/code&gt; statement&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;while&lt;/code&gt; loop is cumbersome when we need to count values or process collections. The &lt;code&gt;for&lt;/code&gt; loop is an easier loop in those cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Execute once, then run while &lt;code&gt;true&lt;/code&gt;: the &lt;code&gt;do-while&lt;/code&gt;loop
&lt;/h2&gt;

&lt;p&gt;A less common iteration option is &lt;a href="https://kodify.net/csharp/loop/do-while/"&gt;C#'s &lt;code&gt;do-while&lt;/code&gt; loop&lt;/a&gt;. This loop repeats code for as long as a condition tests &lt;code&gt;true&lt;/code&gt;. That's similar to the &lt;code&gt;while&lt;/code&gt; loop. But here's what makes &lt;code&gt;do-while&lt;/code&gt; unique: this loop evaluates its condition &lt;em&gt;after&lt;/em&gt; the loop's body executed.&lt;/p&gt;

&lt;p&gt;Since that test happens after the loop's code, code inside the loop always runs at least once. If the loop condition is &lt;code&gt;true&lt;/code&gt; after that first run, the &lt;code&gt;do-while&lt;/code&gt; loop continues. Else it stops. So there's at least one loop cycle, with the possibility of getting more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick example: the &lt;code&gt;do-while&lt;/code&gt;loop in C
&lt;/h3&gt;

&lt;p&gt;Here's a quick example of the &lt;code&gt;do-while&lt;/code&gt; loop:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int i = 0;
    do
    {
        Console.Write(i + " ");
        i++;
    } while (i &amp;lt; 5);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We first declare the &lt;code&gt;i&lt;/code&gt; variable and give it a default of &lt;code&gt;0&lt;/code&gt;. Then we make a &lt;code&gt;do-while&lt;/code&gt; loop. First we use the &lt;code&gt;do&lt;/code&gt; keyword and then a pair of braces (&lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;). Between them we place our code that should repeatedly execute.&lt;/p&gt;

&lt;p&gt;The loop's body has the &lt;code&gt;Console.Write()&lt;/code&gt; method output the current value of &lt;code&gt;i&lt;/code&gt;. Then the increment operator (&lt;code&gt;++&lt;/code&gt;) increases that loop variable with one. That makes the loop condition &lt;code&gt;false&lt;/code&gt; at some point.&lt;/p&gt;

&lt;p&gt;Then we check that loop condition. We use the &lt;code&gt;while&lt;/code&gt; keyword and evaluate whether &lt;code&gt;i&lt;/code&gt; is less than (&lt;code&gt;&amp;lt;&lt;/code&gt;) 5. As long as it is, code inside this loop executes.&lt;/p&gt;

&lt;p&gt;See &lt;a href="https://kodify.net/csharp/loop/do-while/"&gt;C#'s &lt;code&gt;do-while&lt;/code&gt; loop explained&lt;/a&gt; to learn more.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to use C#'s &lt;code&gt;do-while&lt;/code&gt;loop?
&lt;/h3&gt;

&lt;p&gt;In general, the &lt;code&gt;do-while&lt;/code&gt; loop can handle the same situations as the &lt;code&gt;while&lt;/code&gt; loop can. That is, we use &lt;code&gt;do-while&lt;/code&gt; to loop for as long as some condition remains &lt;code&gt;true&lt;/code&gt;, without necessarily knowing in advance how many loop cycles that takes.&lt;/p&gt;

&lt;p&gt;Of course what makes the &lt;code&gt;do-while&lt;/code&gt; loop unique is that it always executes once. But because &lt;code&gt;do-while&lt;/code&gt; is rather uncommon, chances are that people reading your code find this loop harder to understand. Because of that it's a good idea to use &lt;code&gt;do-while&lt;/code&gt; only when you definitely need its unique feature.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strengths and weaknesses of the &lt;code&gt;do-while&lt;/code&gt;loop
&lt;/h3&gt;

&lt;p&gt;These are the strengths of the &lt;code&gt;do-while&lt;/code&gt; loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;do-while&lt;/code&gt; always executes its loop body once. This way other program can safely assume that the loop's code executed.&lt;/li&gt;
&lt;li&gt;Like the &lt;code&gt;while&lt;/code&gt; loop, &lt;code&gt;do-while&lt;/code&gt; loop runs for as long as its condition tests &lt;code&gt;true&lt;/code&gt;. This makes the loop flexible without us specifying in advance how many loop cycles we need.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some disadvantages with the &lt;code&gt;do-while&lt;/code&gt; loop too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When we count with the &lt;code&gt;do-while&lt;/code&gt; loop, we'll have to use a separate variable to control the loop. We have to manage that variable inside the loop in the right way, and not for instance jump over it with &lt;a href="https://kodify.net/csharp/loop/continue/"&gt;the &lt;code&gt;continue&lt;/code&gt; statement&lt;/a&gt;. This requires a bit more work than the same behaviour with the &lt;code&gt;for&lt;/code&gt; loop, for instance.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;do-while&lt;/code&gt; loop is uncommon. That makes mistakes more likely. And increases the odds that people reading your code have trouble understanding the loop well.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;do-while&lt;/code&gt; loop is cumbersome when we need to process elements in a collection. For that the &lt;code&gt;foreach&lt;/code&gt; loop or the &lt;code&gt;for&lt;/code&gt; loop are much better options.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;do-while&lt;/code&gt; loop guarantees that the loop's code always runs once. But that's also possible with a regular &lt;code&gt;while&lt;/code&gt; loop that has its loop condition initialised to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;C# has four types. With &lt;a href="https://kodify.net/csharp/loop/for-loop/"&gt;the &lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt; we count from one value to another. This helps when we process elements in a collection and with math.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://kodify.net/csharp/loop/foreach-loop/"&gt;The &lt;code&gt;foreach&lt;/code&gt; loop&lt;/a&gt; can also iterate through a collection. This loop is however easier to manage, automatically goes through an entire collection, and updates the loop variable automatically.&lt;/p&gt;

&lt;p&gt;If we need to repeat code but don't know how many loop cycles that takes, we use &lt;a href="https://kodify.net/csharp/loop/while-loop/"&gt;the &lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt;. Before each loop cycle, &lt;code&gt;while&lt;/code&gt; checks a true/false condition. When &lt;code&gt;true&lt;/code&gt;, the loop runs; else, the loop ends (or doesn't even start).&lt;/p&gt;

&lt;p&gt;Similar is &lt;a href="https://kodify.net/csharp/loop/do-while/"&gt;the &lt;code&gt;do-while&lt;/code&gt; loop&lt;/a&gt;. This loop also keeps running when a condition tests &lt;code&gt;true&lt;/code&gt;. What's unique, however, is that &lt;code&gt;do-while&lt;/code&gt; always executes once, even when its condition is &lt;code&gt;false&lt;/code&gt; the first time.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
  </channel>
</rss>
