JavaScript - Loops, Conditional Statements, Functions, Variables, Parameters, Arrays, Associative Arrays

Loops in Java


While loop

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Image

For Loop

For loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Image

function HowMany(selectObject) {

..var numberSelected = 0;

..for (var i = 0; i < selectObject.options.length; i++) {

....if (selectObject.options[i].selected) {

......numberSelected++;

....}

...}

..sreturn numberSelected;

While Loop

Do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.

Image

function HowMany(selectObject) {

..var numberSelected = 0;

..for (var i = 0; i < selectObject.options.length; i++) {

....if (selectObject.options[i].selected) {

......numberSelected++;

....}

...}

..sreturn numberSelected;