"From scratch" javascript novice tutorial--2. Branching structures and loops

Source: Internet
Author: User
Tags true true

Introduction of the introduction of JS and vector and operators, we have a preliminary understanding of JS and understanding, and today we take a look at JS commonly used in the branch structure and how the loop structure is used

"Branch Structure in JS"

First, "If-else structure"


1, the structure of the wording:

if (judging condition) {        //condition is true when execution}else{        //condition is false when executing}    

  2, if () the expression, the result of the operation should be:
①boolean:true true False False
②string: Non-empty string is true, empty string is False
③number:0 is false, not 0 are true
④null/nan/undefined: All is False
⑤object: All is True
3, else structure can be omitted according to the specific circumstances

Second, "Multiple if structure (ladder if)"


1, the structure of the wording:

if (condition 1) {        //condition 1 established}else if (condition 2) {        //Condition 1 not established && condition 2 established        //else if part can n multiple}else{        //Condition 1 not established & & Condition 2 not established}            

2, multiple if structure, each judgment condition is mutually exclusive! Only one of the roads can be selected.
3, If/else {} can be omitted, generally not advocated.
If you omit {}, the IF/ELSE structure contains only the most recent line (the semicolon ends).
If you omit {} then the else structure always belongs to the nearest if structure in front of it.

Third, "nested If Structure"


1, the structure of the wording:

if (condition 1) {        //condition 1 established    if (condition 2) {            //condition 1 Set up conditions 2 established    }else{            //conditions 1 Set up conditions 2 not established    }}else{        //conditions 1 not established}

2, if structure can be multi-nesting, but in principle, not more than three layers

Iv. "Switch-case Structure"


1, the structure of the wording:

switch (expression) {case    constant expression 1:    statement 1; break    , Case    constant expression 2:    statement 2; break    ;    Default:    statement n; break    ;}

2. Precautions:
the expression in ①switch (), and the expression following each case, can be any data type supported by JS; (objects and arrays not)
all constant expressions following the ②case must be different, otherwise only the first one will be executed;
the expression constants after ③case can be any data type, and the different case of the same switch structure can be many different data types;
④switch structure in the judgment, the use of a congruent judgment = = =;
⑤break function: After executing the case code, jump out of the current switch structure;
>>> The consequences of missing a break: Start with the right case entry, executing all case and default reasons in turn: ⑥
⑥switch structure in judgment, will only judge the correct answer, when the correct case, will not judge the subsequent project, followed by execution.
⑦switch structures are executed faster than multiple if structures. The switch structure is preferred when branching over multiple routes.

  

As an example:

var num4 = prompt (parseint ("Enter a number")), switch (num4+5) {case 9:document.write ("This is a case block of 9") break;        Case 10:document.write ("This is a case block of 10") Break;default:document.write ("This is the case block of default") break;   

Enter a number, and if you enter 4, print "This is a case block of 9" on the screen.

If you enter 5, a "This is a 10 case block" is printed on the screen.

If it's a different number, print "This is the case block for default" on the screen.

"The loop structure in JS"

first, "Steps of the cyclic structure"
① declaring a loop variable
② Judging cycle conditions
③ Execution Loop Body (all code in while{}) operation
④ updating loop variables
then loop execution ②③④

second, "JS in the loop condition supported data type": All data types (same if)
①boolean:true true False false
②string: Non-empty string is true, empty string is False
③number:0 is false, not 0 are true
④null/nan/undefined: All is false
⑤object: All is true

Third, "While loop"

var n = 1;    Declare the loop variable while (n<=5) {    //judge the Loop condition        document.write ("helloworlds<br/>");    Executes the loop body (all code in while{}) operation n++;    Update loop variable}    

Characteristics: First judge, then execute;
* "Do-while cycle"

  Features: First execution, then judgment; even if the initial conditions are not true, the Do-while cycle is executed at least once.

Iv. "For Loop"

for (Var n=1,j=5;n<=5;n++,j--) {document.write ("hellofor<br/>")}

1, for loop three expressions, respectively: ① define the cyclic variable ② judge the loop condition ③ update the loop variable
Between three expressions, with or as a separator;
    For loop three expressions can be omitted, but two ";" Indispensable
2, for loop characteristics: First judge, then execute;
3, for loop three expressions can have a multi-part, separated by commas, but the second part of the judging condition needs to use && connection, the final result needs to be true/False

V. "Circular control statements"
1, break: Terminate this layer loop, continue to execute the statement behind the loop;
>>> when a loop has multiple layers, break skips only a layer of loops.
2, continue: Skip this cycle, continue to perform the next cycle;
>>> for A For loop, after continue executes, the Loop variable UPDATE statement continues to execute n++
>>> after execution of the while and do-while,continue, the cyclic condition judgment is continued, so when using these two loops, it is important to note that continue must be used after n++.

for (Var n=1;n<=20;n++) {if (n%3==0) {//break;continue;}        document.write (n+ "/");}    

 If it is break, the result of the output is

  

If it is continue, the result of the output is

  

Vi. "Loop Nesting"
The outer loop turns once and the inner layer loops around

for (Var a=1;a<=5;a++) {for (Var b=1;b<=5;b++) {document.write ("*"); } document.write ("<br/>");}

  

Well, it's here today and I hope to help you a little.

"From scratch" javascript novice tutorial--2. Branching structures and loops

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.