The do…while Loop

Control & Loop Statement

Posted on Updated on

JavaScript if…else Statements

While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform right actions.

JavaScript supports conditional statements which are used to perform different actions based on different conditions. Here we will explain if..else statement.

JavaScript supports following forms of if..else statement:

  • if statement
  • ..else statement
  • ..else if… statement.

if statement:

The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.

Syntax:

if (expression){   Statement(s) to be executed if expression is true}

Example:

<script type=”text/javascript”>

var age = 20;

if( age > 18 )

{

document.write(“<b>Qualifies for driving</b>”);

}

</script>

This will produce following result:

Qualifies for driving

if…else statement:

The if…else statement is the next form of control statement that allows JavaScript to execute statements in more controlled way.

Syntax:

if (expression){

Statement(s) to be executed if expression is true

}else{

Statement(s) to be executed if expression is false

}

Example:

<script type=”text/javascript”>

var age = 15;

if( age > 18 )

{

document.write(“<b>Qualifies for driving</b>”);

}else{

document.write(“<b>Does not qualify for driving</b>”);

}

</script>

This will produce following result:

Does not qualify for driving

if…else if… statement:

The if…else if… statement is the one level advance form of control statement that allows JavaScript to make correct decision out of several conditions.

Syntax:

if (expression 1){

Statement(s) to be executed if expression 1 is true

}else if (expression 2){

Statement(s) to be executed if expression 2 is true

}else if (expression 3){

Statement(s) to be executed if expression 3 is true

}else{

Statement(s) to be executed if no expression is true

}

Example:

var book = “maths”;

if( book == “history” ){
document.write(“<b>History Book</b>”);

}else if( book == “maths” ){

document.write(“<b>Maths Book</b>”);

}else if( book == “economics” ){

document.write(“<b>Economics Book</b>”);

}else{

document.write(“<b>Unknown Book</b>”);

}

</script>

This will produce following result:

Maths Book

JavaScript Switch Case

You can use multiple if…else if statements, to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.

Syntax:

The basic syntax of the switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, adefault condition will be used.

switch (expression)

{

case condition 1: statement(s)

break;

case condition 2: statement(s)

break;

case condition n: statement(s)

break;

default: statement(s)

}

The break statements indicate to the interpreter the end of that particular case.

<script type=”text/javascript”>

<!–

var grade=’A’;

document.write(“Entering switch block<br />”);

switch (grade)

{

case ‘A’: document.write(“Good job<br />”);

break;

case ‘B’: document.write(“Pretty good<br />”);

break;

case ‘C’: document.write(“Passed<br />”);

break;

case ‘D’: document.write(“Not so good<br />”);

break;

case ‘F’: document.write(“Failed<br />”);

break;

default:  document.write(“Unknown grade<br />”)

}

document.write(“Exiting switch block”);

//–>

</script>

This will produce following result:

Entering switch block

Good job

Exiting switch block

Example:

Consider a case if you do not use break statement:

<script type=”text/javascript”>

<!–

var grade=’A’;

document.write(“Entering switch block<br />”);

switch (grade)

{

case ‘A’: document.write(“Good job<br />”);

case ‘B’: document.write(“Pretty good<br />”);

case ‘C’: document.write(“Passed<br />”);

case ‘D’: document.write(“Not so good<br />”);

case ‘F’: document.write(“Failed<br />”);

default:  document.write(“Unknown grade<br />”)

}

document.write(“Exiting switch block”);

//–>

</script>

This will produce following result:

Entering switch block

Good job

Pretty good

Passed

Not so good

Failed

Unknown grade

Exiting switch block

JavaScript while Loops

While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need to write loop statements to reduce the number of lines.

JavaScript supports all the necessary loops to help you on all steps of programming.

The while Loop

The most basic loop in JavaScript is the while loop which would be discussed in this tutorial.

Syntax:

while (expression){

Statement(s) to be executed if expression is true }

The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.

Example:

Following example illustrates a basic while loop:

<script type=”text/javascript”>

<!–

var count = 0;

document.write(“Starting Loop” + “<br />”);

while (count < 10){

document.write(“Current Count : ” + count + “<br />”);

count++;

}

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result:

Starting Loop

Current Count : 0

Current Count : 1

Current Count : 2

Current Count : 3

Current Count : 4

Current Count : 5

Current Count : 6

Current Count : 7

Current Count : 8

Current Count : 9

Loop stopped!

The do…while Loop:

The do…while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

Syntax:

do{

Statement(s) to be executed;

} while (expression);

Note the semicolon used at the end of the do…while loop.

Example:

Let us write above example in terms of do…while loop.

<script type=”text/javascript”>

<!–

var count = 0;

document.write(“Starting Loop” + “<br />”);

do{

document.write(“Current Count : ” + count + “<br />”);

count++;

}while (count < 0);

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result:

Starting Loop

Current Count :0

Loop stopped!

JavaScript for Loops

We have seen different variants of while loop. This chapter will explain another popular loop called for loop.

The for Loop

The for loop is the most compact form of looping and includes the following three important parts:

  • The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.
  • The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.
  • The iteration statement where you can increase or decrease your counter.

You can put all the three parts in a single line separated by a semicolon.

Syntax:

for (initialization; test condition; iteration statement){

Statement(s) to be executed if test condition is true

}

Example:

Following example illustrates a basic for loop:

<script type=”text/javascript”>

<!–

var count;

document.write(“Starting Loop” + “<br />”);

for(count = 0; count < 10; count++){

document.write(“Current Count : ” + count );

document.write(“<br />”);

}

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result which is similar to while loop:

Starting Loop

Current Count : 0

Current Count : 1

Current Count : 2

Current Count : 3

Current Count : 4

Current Count : 5

Current Count : 6

Current Count : 7

Current Count : 8

Current Count : 9

Loop stopped!

JavaScript Loop Control

JavaScript provides you full control to handle your loops and switch statement. There may be a situation when you need to come out of a loop without reaching at its bottom. There may also be a situation when you want to skip a part of your code block and want to start next iteration of the look.

To handle all such situations, JavaScript provides break and continue statements. These statements are used to immediately come out of any loop or to start the next iteration of any loop respectively.

The break Statement:

The break statement, which was briefly introduced with the switch statement, is used to exit a loop early, breaking out of the enclosing curly braces.

Example:

This example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write(..) statement just below to closing curly brace:

<script type=”text/javascript”><!–var x = 1;document.write(“Entering the loop<br /> “);while (x < 20){  if (x == 5){      break;  // breaks out of loop completely  }  x = x + 1;  document.write( x + “<br />”);}document.write(“Exiting the loop!<br /> “);//–></script>

This will produce following result:

Entering the loop2345Exiting the loop!

The continue Statement:

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

When a continue statement is encountered, program flow will move to the loop check expression immediately and if condition remain true then it start next iteration otherwise control comes out of the loop.

Example:

This example illustrates the use of a continue statement with a while loop. Notice how thecontinue statement is used to skip printing when the index held in variable x reaches 5:

<script type=”text/javascript”>

<!–

var x = 1;

document.write(“Entering the loop<br /> “);

while (x < 10)

{

x = x + 1;  if (x == 5){

continue;  // skill rest of the loop body

}

document.write( x + “<br />”);

}

document.write(“Exiting the loop!<br /> “);

//–>

</script>

This will produce following result:

Entering the loop

2

3

4

6

7

8

9

10

Exiting the loop!

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements