JS Summary loop exit loop function

Source: Internet
Author: User

While loop

while (condition) {code to execute if condition is set}

* The general condition variable needs to increment, otherwise it will enter the dead loop (infinite loop), the browser crashes or even the computer freezes.

For example, a row-by-line output of 1-100 digits

var i = 1;

while (i<=100) {

document.write (i+ "<br/>");

i++;

}

Do...while Cycle

do{Execute code}while (conditions, if the condition is true, continue the loop, otherwise abort the loop)

For example, a row-by-line output of 1-100 digits

var i = 1;

bo=

document.write (i+ "<br/>");

i++;

}

while (i<=100)

* Unlike while: Do...while is the first to execute code before judging the condition, while the while opposite

For loop

For (initial value of parameter; parameter condition; parameter increment) {Condition for execution code}

Where the parameter is a local variable

For example, a row-by-line output of 1-100 digits

for (Var i=1;i<=100;i++) {

document.write (i+ "<br/>");

}

* It is recommended to use a for loop to facilitate program reading

Terminate loop: break,continue

Break: Exit the entire loop now

Continue: Skipping execution code for the current loop to continue looping

For example:

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

if (i==5) {break}

}

alert (i); Output is 5

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

if (i==5) {continue}

alert (i);

}

Output of 1,2,3,4,6,7,8,9 will find alert (5) skipped

Function

JS has a number of functions, including built-in functions and custom functions.

Built-in functions: in fact, we often use the alert (parameter) can also be seen as a function, it represents the way to pop a window, and the window prompt is the function of the parameter

Custom functions: We can create a function with a specific function method, for example, we create a function area (s,h)//where S is the bottom, H is high

To create a custom function:

Functions Area (s,h) {}//area is a custom function name, S,h is a custom parameter

To add functionality to a function:

function Area (s,h) {

var area=s*h*0.5;

return area;

}

In this way, the area of a triangle with a height of 5 is 10, as long as the function is directly called to write the functions can be: areas (5,10);

JS Summary loop exit loop function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.