title: For Loops and Switches in Swift
body: <p>I'm (relatively) new to programming. I was working through Apple's "Introduction to App Development" and since there isn't an answer booklet of some sort I've had to work things out on my own. However there are some things I'm a bit confused about:</p>
  
  <p>There is a problem I'm working on:</p>
  
  <p>This playground has a Chicken type built in to it. A Chicken has a breed and temper property, and both properties are enums.</p>
  
  <p>Here is an array of chickens:
  chickens = [{silkie, grumpy}...]</p>
  
  <p>The task is to calculate how many chickens of the breed "leghorn" and temper "hilarious" there are in the array. I've come up with the following code:</p>
  
  <pre><code>var chickenOfInterestCount = 0
  
  for chicken in chickens {
      switch chicken.temper {
      case .hilarious:
          switch chicken.breed {
          case .leghorn:
              chickenOfInterestCount += 1
          default:
              chickenOfInterestCount += 0
          }
      default:
          chickenOfInterestCount += 0
      }
  }
  
  
  chickenOfInterestCount
  </code></pre>
  
  <p>It works, but I wonder if there is a more efficient way to do this? Can I make the switch check each chicken for {leghorn, hilarious} and count the number of required chickens directly, rather than using a nested switch? (All of my attempts to try this myself were squashed by scary looking error messages, so I'm guessing no) Following from that, how do for loops work? </p>
  
  <p>When I originally learned for loops, I was under the impression that integers were used to loop through, e.g. the first loop was i = 0, and the code inside performed, then the second loop was i = 1, etc, where "i" could really be anything, and the idea would be the same. Here, "chicken" is used. Why is it that I can set the condition to chicken.temper? What does chicken mean here? Does a for loop respect the type of the array it is looping through? So is what is actually happening here something like "take the 0th Chicken in the array of chickens, call that a 'chicken', then check its temper"?</p>