| Overview |
| |
In this chapter you will learn :
- Importance of looping in programming.
- Different loops available for different situations.
- Break and continue statements.
- Functions used in JavaScript.
- Creating User-defined functions.
- Passing parameters in a function.
- Calling a function.
|
Working with Loops and Functions
Looping refers to the ability of a block of code to repeat itself. JavaScript performs several types of repetitive operations. Using these operations you can iterate through the list of array elements, repeat certain operations until some condition is met, or just keep repeating a certain operation. Loops are also known as control structures. JavaScript offers three types of loop structures:
For Loop
The For loop iterates through a block of statements for a specified number of times until a condition is met. For loop is used when it is known in advance that how many times a particular JavaScript code should run.
| Syntax |
for ( initialize ; condition ; update )
{
JavaScript Statements;
} |
The initialize expression, sets up a counter variable and assigns the initial value. The initialize expression is executed exactly once when the for statement is first encountered. It is the starting point of the loop. The declaration of the counter variable can also be done here. The 'condition' tests the variable for a certain condition. The loop terminates when this condition evaluates as false, i.e., the loop fires till condition evaluates to true. The 'update expression' determines how the variable is incremented. It specifies how the initial value in initialize expression is incremented. This increment factor can be negative also.
| Example |
/* The following code prints the digits from 1 to 10 */
for ( var num = 1; num<=10 ; num++ )
{
document.write (num);
} |
While Loop
The while loop is used when you want the loop to execute infinitely and continue executing until the condition fails.
| Syntax |
while ( condition )
{
JavaScript Statements;
} |
The While loop test the condition for its validity and continues to execute JavaScript statements as long as the condition is true.
| Example |
/* The following code prints the digits from 1 to 10, using while loop*/
var num = 1;
while ( num <= 10 )
{
document.write( num );
num ++;
} |
Do - While Loop
The do-while loop is quite similar to the while loop. The only difference between these two loops is that the do-while loop will execute a block of code only once, whether the condition is true or not whereas the while loop will only execute if the condition is true.
| Syntax |
do
{
JavaScript Statements;
}
while ( condition ) |
In the following example, you can see that even though the condition is false, but the statements inside the 'do' block will execute once.
| Example |
/* The following code prints the digits from 1 to 10, using the do-while loop*/
num = 12;
do
{
document.write ( num );
num++;
}
while ( num <= 10 ) |
Other conditional constructs in JavaScript which offers easy decision making in JavaScript programs are:
|