JS Basic Learning Notes (V)

Source: Internet
Author: User

Multiple options (switch statement)

Switch is more convenient than if else when there are a number of options .

Syntax :

Switch ( expression )

{

Case Value 1:

Executing code block 1

Break

Case Value 2:

Executing code block 2

Break

...

Case Value N:

Execute code block n

Break

Default

With case Value 1 , case value 2...case value n Code that does not execute at the same time

}

Syntax Description :

Switch must assign an initial value, and the value matches each case value. Satisfies All statements after the case is executed and uses the break statement to prevent the next case from running.

Example:

Assuming the evaluation of the student's test results, 10 points out of the system, we will be graded according to each grade, and according to the grade of grades to make different evaluation.

The code is as follows:

Execution Result:

Reviews: Pass, come on!

Note: Remember to add the last break statement after the statement executed by the case. Otherwise, proceed directly to the statement in the following case

Repeat repeat (for loop)

Many things do not just do it once, do it again. If you print ten papers, print one copy at a time, repeat the action until the print is complete. These things, we use loop statements to complete, looping statements, is to repeat the execution of a piece of code.

For statement structure:

For ( initialize variable ; Cycle Conditions ; Loop Iteration )

{

Looping statements

}

Repeatedly (while loop)

There is also a while loop that has the same functionality as the For loop , and the while loop repeats a piece of code until a condition is no longer satisfied.

While statement structure:

while ( judging condition )

{

Looping statements

}

Use the while loop to complete the action of taking the ball from the box, taking a total of 6 balls at a time .

<script type= "Text/javascript" >

var num=1; Initialize Value

while (num<=6)// condition judgment

{

document.write ("Take out the first +num+" <br/> ");

num=num+1; Condition Value Update

}

</script>

To go back and forth (Do...while loop)

The basic principle of the do-while structure is essentially the same as the while structure, but it guarantees that the loop body is executed at least once. Because it is first executed code, after judging the condition.

Do...while statement structure:

Do

{

Looping statements

}

while ( judging condition )

Exit Loop break

Used in while, for, Do...while, while loops the break statement exits the current loop and executes the following code directly.

The format is as follows:

For ( initial conditions ; Judging conditions ; post-cycle conditional value Update )

{

if ( special case )

{break;}

Loop code

}

When a special situation occurs, the loop ends immediately.

Programming exercises (push () method)

In a college programming elective class, we got a group of student data for the class, name, gender, age, and grade, and then we're going to use JavaScript to pick out the names of all the girls who are freshmen.

Student information is as follows:

(‘SmallA ', 'female', +, 'Freshman'), ('SmallB ', 'male', all, 'Freshman), ('SmallC ', 'female', ' said theSeniors'), ('SmallD ', 'female', +, 'Freshman')

Mission Ideas:

The first step: to make an array of these data, so that we operate after the OH. Tip : Use a two-dimensional array.

The second step: Filter the Data bar, first to find all the information is a freshman ;

The third step: Finally again filter the previous step to get data, print out all the names of girls ;

Code:

JS Basic Learning Notes (V)

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.