title: Using an IF statement inside loop vs outside the loop
body: <p>i have a program with a nested nested nested loop (four loops altogether). I have a Boolean variable which i want to affect the code in the deepest loop and in the first nested loop a small amount. My dilemma is, i don't really want to have the if else statement put inside the loops as i would have thought that would check the Boolean's state every iteration using extra time to check the statement, and i know that the Boolean's state would not change when the loop starts. This lead me to think it would be better to place the if else statement outside the loops and just have my loops code slightly changed, however, this also looks messy there is a lot of repeated code.</p>
  
  <p>One thing i thought might work, but of which i have little experience using, is a delegate, i could simply put some of the code in a method and then create a delegate, depending on the state of betterColor i could then assign that delegate methods with the different code on them beforehand, but this also seems messy.</p>
  
  <p>Below is what i am trying to avoid as i thhought it may slow down my algorithm:</p>
  
  <pre><code>for (short y = 0; y &lt; effectImage.Height; y++)
  {
      int vCalc = (y &lt;= radius) ? 0 : y - radius;
  
      for (short x = 0; x &lt; effectImage.Width; x++)
      {
          int red = 0, green = 0, blue = 0;
          short kArea = 0;
  
          for (int v = vCalc; v &lt;= y + radius &amp;&amp; v &lt; effectImage.Height; v++)
          {
              int calc = calcs[(y - v) + radius];
              for (int h = (x &lt;= calc || calc &lt; 0) ? 0 : x - calc; h &lt;= x + calc &amp;&amp; h &lt; effectImage.Width; h++)
              {
                  if (betterColor == true)
                  {
                      red += colorImage[h, v].R * colorImage[h, v].R;
                      green += colorImage[h, v].G * colorImage[h, v].G;
                      blue += colorImage[h, v].B * colorImage[h, v].G;
                      kArea++;
                  }
              }
          }
  
          if (betterColor == true)
              effectImage.SetPixel(x, y, Color.FromArgb(red / kArea, green / kArea, blue / kArea));
          else
              effectImage.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(Math.Sqrt(red / kArea)), Convert.ToInt32(Math.Sqrt(green / kArea)), Convert.ToInt32(Math.Sqrt(blue / kArea))));
      }
  
      if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
      {
          image.Image = effectImage;
          image.Update();
      }
  }
  </code></pre>
  
  <p>And here is what my code now looks like:</p>
  
  <pre><code>if (betterColor == true)
  {
      for (short y = 0; y &lt; effectImage.Height; y++)
      {
          int vCalc = (y &lt;= radius) ? 0 : y - radius;
  
          for (short x = 0; x &lt; effectImage.Width; x++)
          {
              int red = 0, green = 0, blue = 0;
              short kArea = 0;
  
              for (int v = vCalc; v &lt;= y + radius &amp;&amp; v &lt; effectImage.Height; v++)
              {
                  int calc = calcs[(y - v) + radius];
                  for (int h = (x &lt;= calc || calc &lt; 0) ? 0 : x - calc; h &lt;= x + calc &amp;&amp; h &lt; effectImage.Width; h++)
                  {
                      red += colorImage[h, v].R * colorImage[h, v].R;
                      green += colorImage[h, v].G * colorImage[h, v].G;
                      blue += colorImage[h, v].B * colorImage[h, v].G;
                      kArea++;
                  }
              }
  
              effectImage.SetPixel(x, y, Color.FromArgb(Convert.ToInt32(Math.Sqrt(red / kArea)), Convert.ToInt32(Math.Sqrt(green / kArea)), Convert.ToInt32(Math.Sqrt(blue / kArea))));
          }
  
          if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
          {
              image.Image = effectImage;
              image.Update();
          }
      }
  }
  else
  {
      for (short y = 0; y &lt; effectImage.Height; y++)
      {
          int vCalc = (y &lt;= radius) ? 0 : y - radius;
  
          for (short x = 0; x &lt; effectImage.Width; x++)
          {
              int red = 0, green = 0, blue = 0;
              short kArea = 0;
  
              for (int v = vCalc; v &lt;= y + radius &amp;&amp; v &lt; effectImage.Height; v++)
              {
                  int calc = calcs[(y - v) + radius];
                  for (int h = (x &lt;= calc || calc &lt; 0) ? 0 : x - calc; h &lt;= x + calc &amp;&amp; h &lt; effectImage.Width; h++)
                  {
                      red += colorImage[h, v].R;
                      green += colorImage[h, v].G;
                      blue += colorImage[h, v].B;
                      kArea++;
                  }
              }
  
              effectImage.SetPixel(x, y, Color.FromArgb(red / kArea, green / kArea, blue / kArea));
          }
  
          if (y % 4 == 0) // Updates the image on screen every 4 y pixels calculated.
          {
              image.Image = effectImage;
              image.Update();
          }
      }
  }
  </code></pre>
  
  <p>In terms of what the code does, it is a box blur that uses a circular kernal.</p>