www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Using break and continue statements Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49
In the last three lessons we have learned to use looping constructs in JavaScript. In this lesson we are going to learn two very important JavaScript statements i.e. break and continue that are used in conjunction with the looping constructs.

break Statement
The break statement in JavaScript, as its name implies, is used to break or terminate a loop or a label block in a switch statement, immediately. Whenever the execution reaches a break statement, written inside a loop, the loop is terminated, immediately. Same is the case with the label blocks of switch statements.

The syntax of the break statement follows:

break;

Code example follows:

for (i = 0; i < 10; i++)
{
if (i == 5) {
break;
}
document.write("Pickatutorial.com")
}

The above code prints "Pickatutorial.com" 5 times, instead of 10! Because when the the value of i equals 5 the if condition becomes true and the if code block executes the break statement, that immediately breaks the loop.

continue Statement
The continue statement can be used to restart a while, do-while or for loop. Inside a loop whenever the execution reaches a continue statement the remaining code of the current iteration of the loop is not executed and, instead, the loop continues in its next iteration.

When we use continue statement within a loop, it terminates the current iteration of the innermost enclosing while, do-while or for loop and continues execution of the loop with the next iteration. Notice that this is in contrast to the break statement, that termintes the loop altogether.

The syntax of the continue statement follows:

continue;

Code example follows:

for (i = 0; i < 10; i++)
{
if (i == 5) {
continue;
}
document.write(i + ". Pickatutorial.com </br> ")
}

When the above code is executed its output would look like:

0. Pickatutorial.com
1. Pickatutorial.com
2. Pickatutorial.com
3. Pickatutorial.com
4. Pickatutorial.com
6. Pickatutorial.com
7. Pickatutorial.com
8. Pickatutorial.com
9. Pickatutorial.com


Notice that the above loop prints nothing when the value of i equals 5, because of the continue statement.


Next >>> Lesson No. 15: JavaScript Functions





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.