Javascript6

«<Javascript5|Javascript7»>

Looping

while

This loop continues while the given condition is found true else the loop gets terminated. The variable meets the condition at the gate of while loop if the variable do not fulfill the condition then the loop will not be executed for even a single time.

syntax:

While (condition ){

code to be executed

}

[Top]

do – while

This loop continues while the given condition is found true else the loop gets terminated, the difference in this and while looping is even the condition is false but this loop gets executed at least once but while loop gets terminated at initial level if condition is not satisfying.

syntax:

do{

code to be executed

}While (condition )

[Top]

For

This loop contains the initial value, termination value , increment/decrement value in a single line, which in on other looping are used in separate line, the scope of a for loop is inside the curly brackets used with it. The initialization segment is executed only once at the start of the loop and termination and increment/decrement segments are executed every time till the condition remains true.

syntax:

for(initialization; termination; increment/decrement){

code to be executed

}

[Top]

break and continue

These statements are used with in a loop. Break as the name suggests is used to break or terminate the loop in between & continue is to continue the loop breaking it for current loop value.

break:
This statement breaks the loop & shifts the control out of the loop.

continue:
This statement breaks the loop for current value and resumes the loop with new value.

for syntax refer the example…
[Top]

for - - in

This loop is used with arrays and objects , to access their elements and properties respectively.

In for..in looping the variable starts from zero and increases itself with one until it reach to the length of array.

Syntax:

for( variablename in objectname){

lines to be executed

}

[Top]

Example

sample code

<html>
<head></head>
<body>
<script type="text/javascript">

var count;
for(count=0;count<100;count++){

if (count<10){
continue
}
if (count>20){
break
}

document.write(" "+count)

}
var color;
var shop=new Array()
shop[0]="Red"
shop[1]="Blue"
shop[2]="Green"
shop[3]="Yellow"
shop[4]="Magenta"
for(color in shop){

document.write("<br> "+shop[color]);

}

</script >
</body>
</html>

Understanding program:
Value of count is initially 00 , the condition with for loop is while count is lesser than 100 the statements typed in thebox of for loop should be executed.But because of continue command first 10 digits are not printed and then bcz f break command loop terminates aftr 20.C implementation to understand bettr.

Color is a undefined variable and shop is an array, when color is kept as a subscript of array shop then the color variable becomes of number type and from 0 to 4 gets incremented by for – in loop. It is increased up to 4 only because shop array have 5 elements and an array start from 0 position.

click loop to C above code's implementation in browser…
[Top]
«<Javascript5|Javascript7»>

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.