Loops & Statements in JavaScript

1. If & If-else:

Syntax:
If (condition)
{   statement  }
else
{   statement  }

e.g.
<html>
<head>
<script>
if (confirm ("Would you like to learn JavaScript"))
{
alert ("You have clicked OK ");
}
else {
alert ("You have clicked Cancel");
}
</script>
</head>
</html>                                                                                                         

2. for:

Syntax:

for ( [variable=initial condition]; [condition]; [increment/decrement expression] )
{
   statement
}

e.g. 

<html>
<head>
<script>
for (var i = 0; i < 10; i++)
{
document. write ("Welcome to world of JavaScript");
}
</script>
</head>
</html>                                                                      

 

3. While:

Syntax:

while (condition)
{
    statement
}

e.g.

<html>
<head>
<script>
var i=1;
while (i < = 10) 
{
document.write ( i );
i++;
}
</script>
</head>         
</html>                                                              


4. Break & Continue:


Syntax:

break;
/ continue; (to be written inside a loop e.g. for loop or while loop or if-else loop).

The difference between Break & Continue statement is: the break statement terminates
the current while or for loop and transfers the control to the statement that follows the
terminated loop.
whereas the continue statement stops executing the statements in a while or a for loop
and skips to the next iteration of the loop. It doesn't stop altogether like the break statement;
instead, in a while loop, it jumps back to the condition. In a for loop it jumps to the update
expression.

Back