Type of loops

  1. for() loop

    The for loop repeats the code inside the squgly brackets ({}) until a certain condition is met. In the sytax for the for() loop you have to provide the following parameters separated by a semi-colon:

    • variable - you can either define a variable here, or specify a previously defined variable. For exampe, var i = 0, or just i, where i has been previously defined and has a pre-existing value.

    • criteria to continue looping. For example, i < 5

    • how to manipulate the variable with each iteration - for e.g. increment i by one, which by the way can be specified as i = i + 1, or i++

    An example of the for() loop will be "for(var i = 0; i<5; i++){ }"

  2. while() loop

    Unlike the for() loop in which yuo have to specify three parameters, in the while loop you only specify one pararmet, viz. the condition. The code in the squgly bracket { } keeps executing until the condition is met.

    An example of the while loop() will be "while ( i <10 ){ }"