Latest News

Conditional Statements in Javascript,conditional statements


Conditional Statements: If any statement executed depends on a condition then that statement are called conditional statement. It is also known as control structures.
Java Script supports six types of conditional statements.
They are:
  • If
  • If-else
  • Nested if-else
  • Else-if-ladder
  • Switch
  • Ternary
If: If condition will execute when condition is true.
Syntax:
if(condition)
{
//statements
}
Example:
if(age<18)
{
document.write(‘miner’);
}

If-else: If condition is true, if lock will execute and if condition is false else block will execute.
Syntax:
if(condition)
{
statement;
}
else
{
statement;
}
Example:
if(age<18)
{
document.write(‘minor’);
}
else
{
document.write(‘major’);
}

Nested if-else: It is used to verify multiple conditions depends on the main condition.
Syntax:
if(main)
{
if(condition 1)
{
//statement
}
else
{
//statement
}
if(condition2)
{
//statement
}
else
{
//statement
}
}
Else-if ladder: It is also used to verify multiple conditions
Syntax:
if(condition1)
{
}
else if(condition2)
{
}
else if(condition3)
{
}
Example:
document.write(“else-if ladder”);
if(age<=10&&age>=0)
{
document.write(“child”);
}
else if(age<=20&&age>10)
{
document.write(“teenage”);
}

Switch: It is also similar to else-if ladder.
Syntax:
switch(condition)
{
case1: statements;
break;
case2: statements;
break;
}
Example:
document.write(“switch condition”);
switch(age)
{
case(“0”):
document.write(‘child<br>’);
break;
case(“10”):
document.write(‘teenage<br>’);
break;
default:
document.write(‘no record found’);
}

No comments:

Post a Comment

eWeb Classes Powered by MV Innovative InfoTech Copyright © 2014

Theme images by Bim. Powered by Blogger.