Topics covered in this snack-sized chapter:

JavaScript Statements

Types of Statements

Variable Declaration Statements

Conditional Statements

Looping Statements

JavaScript Statements 

JavaScript supports a compact set of statements that can be used for interactivity in web pages.

A statement can set a variable equal to a value.

Statements define what the script will do and how it will be done.

A statement can also be a function call, i.e. document.write().

JavaScript program is controlled by statements, which are executed for their effect and do not have values.

Types of Statements

JavaScript statements are divided into six categories:

Variable declaration statement

Conditional statement

Loop statement

Object manipulation statement

Comment statement

Exception handling statement

Variable Declaration Statements

In JavaScript keyword "var" is used to declare a variable. There is no specific data type in JavaScript so any variable is created using var.

Syntax:

var variable1;// the name of the variable being declared.

Example

<HEAD>

<script type="text/Javascript">

    var money;

    var name;

</script>

</HEAD>

Conditional Statements

Conditional statements are divided into following categories:

1. if Statement:

if statement execute a statement if a logical condition is true.

Syntax :

if (condition)

{

 code to be executed if condition is true

}

Example

<script type="text/javascript">

if (income > 100000)

{

 alert ('We can offer you a special benefit.');

}

</script>

2. if… else Statement:

Use  if....else statement to execute some code if a condition is true and another code if the condition is false.

Syntax :

if (condition)

{

  code to be executed if condition is true

}

else

{

  code to be executed if condition is not true

}

Example

<script type="text/javascript">

if (marks<20)

{

 x="poor performance";

}

else

{

 x="average performance";

}

</script>

3. if… else if… else Statement:

Use if.... else if…else statement to select one of several blocks of code to be executed.

Syntax :

if (condition 1)

{

  code to be executed if condition 1 is true

}

else if (condition 2)

{

  code to be executed if condition 2 is true

}

else

{

  code to be executed if condition 1 and condition 2 are not true

}

Example

<script type="text/javascript">

if (marks<10)

{

  x="fail";

}

else if (marks<20)

{

  x="poor";

}

else

{

  x="good";

}

</script>

Looping Statements

Loop statements are divided into following categories:

1. for Statement:

for statement includes the following important points:

Loop initialization: where we initialize our counter to a starting value.

Test statement: which will test whether the given condition is true or not. Test result will determine whether loop will continue or terminate.

The iteration statement: where you can increase or decrease your counter.

Syntax :

for (initialization; test condition; iteration statement)

{

  Statement to be executed if test condition is true

}

Example

<script type="text/javascript">

for (var i = 0; i <= 9; i++)

{

  document.write (i);

 document.write (" ");

}

</script>

Output:

0 1 2 3 4 5 6 7 8 9

2. while Statement:

It executes a statement or series of statements until a specified condition is false.

Syntax :

while (expression)

{

 Statement to be executed if expression is true

}

Example

<script type="text/javascript">

var i = 0;

var j = 10;

while (i < 100)

{

    if (i == j)

    break;

    i++;

}

document.write(i);

</script>

Output:

10

3. do… while Statement:

It executes a statement block once, and then repeats execution of the loop until a condition expression evaluates to be false.

Syntax :

do

{

Statement;

}

while (expression);

Example

<script type="text/javascript">

var i = 0;

do

{

    document.write (i + " ");

    i++;

}

while (i < 10);

</script>

Output:

0 1 2 3 4 5 6 7 8 9

4. Label Statement:

A label is simply an identifier followed by a colon that is applied to a statement or block of code.

Syntax

1

2

label:

Statements;

Example

<script type="text/javascript">

Outer:

for (i = 1; i <= 10; i++)

{

 document.write ("

 ");

 document.write ("i: " + i);

 document.write (" j: ");

 Inner:

 for (j = 21; j <= 30; j++)

 {

  if (j == 24)

  {

   continue Inner;

  }

  document.write (j + " ");

 }

}

</script>

Output: see... practically

5. Break Statement:

Break Statement is used to exit a loop early, breaking out of the enclosing curly braces.

Syntax

1

break;

Example

<script type="text/javascript">

for (var i = 1; i < 100; i++)

{

  if (i == 15)

  {

  break;

  }

  document.write (i);

  document.write (" ");

}

</script>

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

6. Continue Statement:

The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block.

Syntax :

1

continue;

Example :

<script type="text/javascript">

for (var i = 1; i < 10; i++)

{

 if (i < 5)

 {

  continue;

 }

document.write (i);

document.write (" ");

}

</script>

Output:

 5 6 7 8 9

7. Return Statement:

It exits from the current function and returns a value from that function.

Syntax :

1

return[(expression)];

Example :

<script type="text/javascript">

function myfunction(arg1, arg2)

{

 var r;

 r = arg1 * arg2;

 return(r);

}

</script>

8. switch Statements:

It is used to select one of many blocks of code to be executed.

It is used to provide a short syntax for multiple equality tests.

Basically it is an enhanced version of the "if-else" statement that is more convenient to use, when you have code that needs to choose a path from many to follow.

Syntax :

switch (expression)

{

case value1:

statement;

break;

case value2:

statement;

break;

"

"

default : statement;

}

Example :

function mycolor(color)

{  

 switch(color)

  {

   case "Red":

    alert ("It is just like a Tomato");

    break;

   case "Green":

    alert ("it is just like a tree");

    break;

   default:

    alert("Choose yourself  then…….");

  }

}


Translate

CR Zaman. Powered by Blogger.

Recent Posts

Popular Posts