Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Repeating tasks with loops

 

Loops is maybe the most useful and most fundamental concept of structured programming. If it wasn't for loops it would be impossible to achieve all the things that we have done till today, not only in the web industry but in programming in general. 

Loops give us the opportunity to write and execute the same block of code repeatedly, as long as we specify. Let's take a look at our problem.

We have 10 users that they have enrolled themselves to our application. Everyday i want to check if some of them, one of them or maybe all of them have birthday today. This is the condition i need to check. I need to compare if the date today matches the day that they have given us as a birthday. If this is true then i give the user the premium account for the week. If not, nothing happens. 

Luckily by creating a loop we don't need to write these conditions 10 times. We create a loop that runs 10 times in this case and we check for the same condition once by changing the date of birth of every user for our comparison.

Of course, in order for this example to work, we need to have a place where we store all the birthdays for every user (something like an JS Array), but that comes later.