JavaScript Beginner Chapter _4 (loops and Functions)

Source: Internet
Author: User

seven, Circular Statements
1.While
Grammar:

 while (exp) {     //statements;}

Description

 while (variable <= End Value) {     

Cases:

var i=0;  while (i<=9) {        // use a loop to output 9 photos on the page    document.write (' );        I+ +;      }

2. do...while

The Do...while function is the same as while, except that the do...while is executed after the execution, while Vice Versa.

syntax:

 do {     //statements;}  while (condition);

Description

 do {     while (variable <= end value);

Cases:

var i=0;  do {     // using loops to output 9 photos     document.write ('  " );     I+ +;}  while (i<=9);

3.for Loop Statement
Grammar:

 for (initial; expression; post-loop-expression) {    //}

Description

 for (variable = start value; variable <= end value; variable = variable + step value) {     

Cases:

4. Break statement and Continue statement

The break statement can exit the Loop immediately;
the continue statement simply exits the current loop;
Cases:

 for (var i = 1; i < i++) {     if(i = = 4) {         continue//  Exit the loop, and the remaining loop continues to execute         }     if(i = = 7)        {break;    // terminates the loop, after which the remaining loops are not executing     }    

seven, function

description: A function is a set of statements that completes a function, defined by a keyword function + function name + a set of parameters; function in a fixed

After righteousness can be repeated calls, often used to write functions as a function, the use of functions can make the structure of the code more clear.
1. Basic Syntax:

function funname (arg0, arg1, ... argN) {     //}

Use of 2.return

Return a value from a function using the return statement

function sum (num1, num2) {     return num1 +var s = Sum (1, 2); alert (s);      // result is 1+2=3;

Attention:

1. Any code that is behind the return statement will never be executed!
2. In javascript, you don't have to specify a return value!
3. When defining the parameters of a function, if the function is called without passing arguments, it does not give an error, but it can be judged in the function whether there is a pass-in argument, for example:

if (variable = = 'undefined ' | | variable = = Null) {         variable= ' 1 ';    //can give him a default value }

JavaScript Beginner Chapter _4 (loops and Functions)

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.