Loops & iterations in javascript part 1

Loops offer a quick and easy way to do something repeatedly.

Various loop mechanisms offer different ways to determine the start and end points of the loop.

There are various situations that are more easily served by one type of loop over the others.

LOOP STATEMENTS

  • for
  • do…while
  • while
  • labeled
  • break
  • continue
  • for…in
  • for…of

FOR STATEMENT

A for loop repeats until a specified condition evaluates to false.

* * *

for ([initial Expression]; [conditionExpression];

[increment Expression])

statement

* * *

function howMany (selectobject) {

let numberSelected = 0;

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

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

numberSelected++;

}

}

return numberSelected ;

}

 DO..WHILE STATEMENT

The do…while statement repeats until a specified condition evaluates to false.

* * *

do

statement

while (condition) ;

* * *

let i = 0;

do {

i + = 1;

console.log(i);

}while (i < 5);

WHILE STATEMENT

A while statement executes its statements as long as a specified condition evaluates to true.

* * *

while (condition)

Statement

* * *

Let n = 0;

Let x = O;

while (n < 3) {

n++;

x + n;

}

Leave a Reply

Your email address will not be published.