Sunday, 11 August 2013

javascript Conditional statements

Conditional statements:
These statements are used to execute a block of code (statements) based on a condition.

1.       If statement
2.       If..else statement
3.       Switch statements
Iterative statements
1.       While
2.       Do..while()
3.       For()

1. if statement:
it is executed only when the condition is true
syntax:
if(condition)
{
  statement;
}
Ex:
<html>
 <head>
  <script type="text/javascript">
  if(2<7)
  {
   document.write("2 is smaller value");
  }
 </script>
 </head>
 </html>

2. if..else statements

 If the condition is true we execute if block otherwise we execute 'else' block.

syntax:
 if(condition)
 {
    statement1;
 }
else
{
  statement2;
}
Ex:
<html>
 <head>
  <script type="text/javascript">
   if(2<1)
   {
    document.write("2 is smaller value");
   }
   else
   {
    document.write("2 is bigger value");
   }
  </script>
 <head>
</html>
3. switch statement

the switch statement is used to execute a block of code from multiple blocks of codes.

Ex:
<html>
 <head>
  <script type="text/javascript">
  var no=parseInt(prompt("enter a number"));
  switch(no)
  {
   case 1: document.write("one");
          break;
   case 2: document.write("two");
          break;
   case 3: document.write("three");
          break;
   default: document.write("default");
  }
 </script>
 </head>
</html>
output:


 if you type any number (ex: 1)
it displays like below
Iterating statements:

No comments:

Post a Comment