title: Looping with double vs int
body: <p>I am trying to figure out why i get two different outputs for seemingly similar loops. I think it has something to do with looping with doubles versus ints, but I am not sure. </p>
  
  <p>This code outputs 4:</p>
  
  <pre><code>int num = 10;
  int cost;
  int counter = 0;
  
  for (cost = 1; cost &lt;= num; cost += 1){
      counter += 1;
      num -= cost;
  }
  
  cout &lt;&lt; counter &lt;&lt; endl;
  </code></pre>
  
  <p>This is the output for this section:</p>
  
  <pre><code>Current Cost: 1
  Remaining: 9
  Current Cost: 2
  Remaining: 7
  Current Cost: 3
  Remaining: 4
  Current Cost: 4
  Remaining: 0
  4 candies; 0 left over
  </code></pre>
  
  <p>This code outputs 3:</p>
  
  <pre><code>double num = 1.0;
  double cost; 
  int counter = 0;
  
  for(cost = 0.1; cost &lt;= num; cost += .1) {
      counter +=1;
      num -= cost;
  }
  
  cout &lt;&lt; counter &lt;&lt; endl;
  </code></pre>
  
  <p>Edit:</p>
  
  <p>This is the output for this code when I tried debugging it:</p>
  
  <pre><code>    Current Cost: 0.1
      Remaining: 0.9
      Current Cost: 0.2
      Remaining: 0.7
      Current Cost: 0.3
      Remaining: 0.4        
      3 candies; 0.400000 left over
  </code></pre>
  
  <p>Why is there a difference between the two?</p>