While Loops vs. For Loops

I’m still half forgetting the difference between while loops and for loops most of the time (and when to use each one), while working in both my Codecademy JavaScript course and my Udacity Introduction to Computer Programming course. Since I’m starting the while loops section on Codecademy now, might as well hammer out the difference once and for all, so I don’t have to keep leafing through my notes.

According to Codecademy introduction to while loops: “‘While’ loops are like ‘for’ loops, only they don’t need a counter—they continue to run until their condition is met.”

Mathbits.com (forgiving its goofy notebook-in-space motif) also offers a clear differentiation:

  • while loops must repeat until a certain condition is met
  • for loops are repeated for a specific number of times

With these in mind, here’s a reminder example I’ve made myself to make the purpose of each stick in my head:

  • Ke$hify For Loop: For length of ‘Kesha’, replace ‘s’ with $
  • Gaga While Loop: While naming songs, if “Gaga” is artist, add song to playlist