Easy Learning JavaScript Seven: Process Control statements for JavaScript

Source: Internet
Author: User

JS Core ECMAScript rules of the flow control statements and other programming languages are quite similar. We choose some practical examples to see

These statements. Sequential structure We are no longer mentioned here, directly saying conditions and loops and other statements.

A conditional selection structure

Conditional selection statements are used to perform different actions based on different conditions, usually when writing code, there is always a need for different decisions to perform different

Moving You can use conditional statements in your code to complete the Services.

In JavaScript, we can use the following conditional statements:

If statement: Use this statement to execute code only if the specified condition is true

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The result of the operation is:


If...else statement: Executes code when the condition is true and executes other code when the condition is false

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The result of the operation is:


If...else If....else Statement: Use this statement to select one of several code blocks to execute

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The result of the operation is:


Switch statement: Use this statement to select one of several code blocks to execute. Switch statements are used to perform different actions based on different conditions

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Results of the operation:


Use of the default keyword

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The result of the operation is:

Two-cycle structure

A loop can execute a code block for a specified number of times.

JavaScript supports different types of loops:

(1) For statement: Loop code block a certain number of times

for (Var box=1;box<=10;box++) {    document.write ("box=" +box+ "<br/>");}

The result of the operation is:


(2) for...in statement: Looping through the properties of an object

    var box={       name: "Zhang San",       age:24,       sex: "Male"     };    For (x in box)    {       document.write (box[x]+ "<br/>");    }

The result of the operation is:


(3) While statement: loops the specified block of code when the specified condition is true. It is more practical to judge and execute the statements first.

     var box=1;     while (box<=5)     {       document.write ("box=" +box+ "<br/>");       box++;     }

The result of the operation is:


(4) Do...while-The specified code block is also looped when the specified condition is true. Execute once and then judge

     var box=1;     do{       document.write ("box=" +box+ "<br/>");       box++;     } while (box<=10)

The result of the operation is:


Three other statements

(1) Break statement: Used to jump out of a loop.

    for (Var box=1;box<=10;box++)     {        if (box==5)       {          break;//forces the entire loop to exit        }       document.write ( "box=" +box+ "<br/>");     }

The result of the operation is:


Execution to the fourth cycle no longer executes, jumping out of the cycle, the output of less box=5 after the cycle.

(2) Continue statement: Used to skip an iteration in the loop.

     for (Var box=1;box<=10;box++)     {       if (box==5)       {          continue;//exits the current loop and continues with the following loop        }       document.write ("box=" +box+ "<br/>");     }  

The result of the operation is:


Execute to the fourth cycle, jump out of the fifth cycle, continue to the following, output less box=5.

(3) With statement: Set the scope of the code to a specific object

Let's start by looking at how we're going to output the value of an object's properties:

      var box={       name: "Zhang San",       age:24,       sex: "Male"      };      var n=box.name;      var a=box.age;      var s=box.sex;      document.write (n+ "<br/>");  document.write (A + "<br/>");  document.write (s);

The result of the operation is:


Use the WITH statement to write:

     var box={       name: "Zhang San",       age:24,       sex: "Male"      };      With (box) {        var n=name;        var a=age;        var s=sex;      };  document.write (n+ "<br/>");  document.write (A + "<br/>");  document.write (s);

The result of the operation is:



Easy Learning JavaScript Seven: Process Control statements for JavaScript

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.