Control statements and loops in Java script

Java script supports control statements.They are:

  1. Conditional Statements(If,if …else,if…else…if,switch)
  2. Loop statements/Iterations(For loop,While loop)

Syntax for If statement:

if(condition)
{
document.write(“statement”);
}

Syntax for if…else statement:

if(condition)
{
document.write(“statement”);
}
else
{
document.write(“statement”);
}

Syntax for else if statement:

if(condition)
{
document.write(“statement”);
}
else if(condition)
{
document.write(“statement”);
}
else(condition)             //here we may or may not write condition
{
document.write(“statement”);
}

Syntax for Switch statement:

switch(variable)
{
case outcome 1:
{
document.write(“…..”);
break;
}
case outcome 2:
{
document.write(“…..”);
break;
}
default:
{
document.write(“…..”);
}
}

Break:

Writing the break inside the control structure will cause the programme to jump end of the block.Control statement resume after the block,as if block had finished.

Example to explain above(If statements…)

onditonal statement

Output:

conditional stat output

Example to explain about Switch Statement:

switch

Output:

switch output

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment