Thursday, June 21, 2012

Java Script Tutorial 11 : JavaScript For Loop

When you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform tasks. There are two different kind of loops, we use in JavaScript, these are as follows :
  1. for 
  2. while
We discuss only for loop in this tutorial.


The for Loop :
The for loop is used when you know in advance how many times the script should run. Syntax of for loop is as follow :


for ( variable = startvalue; variable<endvalue;variable = variable + increment)
  {
       code to be executed
  }


For example


for (i=0; i<3; i++)
  {
  y=y + "The number is " + i + "<br />";
  }


This for loop runs 3times, and write results in every new line. The result of above code will be :


The number is 0
The number is 1
The number is 2

Java Script Tutorial 10 : JavaScript Popup Boxes

There are three kind of popup boxes in JavaScript
  • Alert box
  • Confirm box
  • Prompt box
Alert Box :
An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. Syntax of Alert Box is as follows :

javascript alert box
Javascript alert box.
For example
javascript alert box in html
Javascript alert box in html.
Confirm Box :
A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. Syntax of Confirm Box is as follows :

javascript confirm box
Javascript confirm box.
For example

javascript confirm box in html
Javascript confirm box in html.
Prompt Box :
A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax of Prompt Box is as follows :

javascript prompt box
Javascript prompt box.
For example

javascript prompt box in html
Javascript prompt box in html.
To display line breaks inside a popup box, use a back-slash followed by the character n. For example

javascript alert box with line break
Javascript alert box.

Java Script Tutorial 9 : JavaScript Switch Statement


The switch statement are used to perform different action based on different conditions. The switch statement can be replaced by performing many if-then-else-if conditions, but when the answer of an evalution can give you many different answers, the switch-case statement can be more efficient. Syntax of switch statement is as follow :


switch(x)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if x is different from case 1 and 2
}


The value of the variable x will be compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. For example


switch ( day )
{
case 0:
  y="Sunday";
  break;
case 1:
  y="Monday";
  break;
case 2:
  y="Tuesday";
  break;
}


If the value of variable day is 0 then result will be "Sunday", and result will be "Monday" and "Tuesday" if values will be 1 and 2 respectively.


We use the default keyword to specify what to do if there is no match. For example, there is no value equal to 0, 1 or 2, then write a default message, for example write "friday".


switch ( day )
{
case 0:
  y="Sunday";
  break;
case 1:
  y="Monday";
  break;
case 2:
  y="Tuesday";
  break;
default:
  y="Friday";
}


If there is no match found then result will be "friday".

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".

Wednesday, June 20, 2012

Java Script Tutorial 7 : Comparison and Logical Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values. Comparison operators can be used in conditional statements to compare values and take action depending on the result. There are many Comparison operators used in javascript, some of these are below :


Operator Symbol Uses/Description
= = is equal to
= = = is exactly equal to (value and type both)
! = is not equal
> is greater than
< is less than
> = is greater than or equal to
< = is less than or equal to


Logical operators are used to determine the logic between variables or values. We can use following logical operators in java script.


&& ( and  )
|| ( or )
! ( not )


conditional operator : JavaScript contains conditional operator that assigns a value to a variable based on some condition. Syntax of this conditional operator is as below.


somevariablename = (put condition here) ? value1 : value2


If condition meets then output 'll be 'value1', and if condition doesn't meet then output 'll be 'value2'.