Try my app Expense Manager @ google play and amazon store.

Thursday, June 21, 2012

Java Script Tutorial 8 : If Else Statements


Conditional statements are used to perform different actions based on different conditions. In JavaScript we have the following conditional statements.

  • if statement
  • if...else statement
  • if...else if....else statement
  • switch statement

If Statement :
We use the if statement to execute some code only if a specified condition is true. Syntax of if statement is as follow :


if (some condition)
  {
  code to be executed if condition is true
  }


For example


if (number<33)
  {
  x="failed";
  }


If value of variable number is less then 33 then result will be "failed".


If  .. else Statement :
We use the if  else statement to execute some code if a condition is true and another code if the condition is not true. Syntax for this is as follows :


if (some condition)
  {
       code to be executed if condition is true
  }
else
  {
       code to be executed if condition is not true
  }


For example


if ( number < 33 )
  {
  y="failed";
  }
else
  {
  y="passed";
  }


If the value of variable number less then 33 then result will be "failed", otherwise result will be "passed".


If .. else if  .. else Statement :
We use the if  else if  else statement to select one of several blocks of code to be executed. Syntax for this is as follow


if (condition 1 here)
  {
       code to be executed if condition1 is true
  }
else if (condition 2 here)
  {
       code to be executed if condition2 is true
  }
else
  {
       code to be executed if neither condition1 nor condition2 is true
  }


For example


if (number>90)
  {
  y="Passed : Excellent Result";
  }
else if (number<33)
  {
  y="failed";
  }
else
  {
   y="passed";
  }


If the value of variable number will be greater then 90 then result will be "Passed : Excellent Result". if value will be less then 33 then result will be "failed", otherwise (when value will be between 33 and 90 ) result will be "passed".

No comments:

Post a Comment

Follow by Email

Google+ Badge