JavaScript Learning Five

Source: Internet
Author: User

Make a judgment (if statement)

The IF statement is the statement that is used when the code is executed based on the condition.

Grammar:

if (condition) {Execute code when condition is set}

Note: If lowercase, uppercase letters (IF) will go wrong!

Suppose you apply for Web front-end technology development post, if you will HTML technology, your interview success, welcome to join the company. The code is represented as follows:

<script type= "Text/javascript" >  var mycarrer = "HTML";  if (Mycarrer = = "HTML")  {    document.write ("You are successful in the interview, welcome to join the company.") ");  } </script>

//=================================================================

Two Choice one (If...else statement)

The If...else statement executes the code when the specified condition is true and executes the else code when the condition is not established.

Grammar:

if (condition) {The code executed when the condition is established}else{the code executed when the condition is not established}

Suppose you apply for Web front-end technology development post, if you will HTML technology, your interview success, welcome to join the company, or you do not succeed in the interview, can not join the company.

The code is represented as follows:

<script type= "Text/javascript" >  var mycarrer = "html";//mycarrer variable store skill  if (Mycarrer = = "html")    { document.write ("You are successful in the interview, welcome to join the company. ");  }  else  //Otherwise, the skill is not HTML    {document.write ("Your interview is unsuccessful and you cannot join the company.") ");} </script>

/=========================================================================

Multiple judgments (if.. else nested statements)

To select a group to execute in multiple sets of statements, use the IF: else nested statements.

Grammar:

if (condition 1) {condition 10% executes immediately code}else  if (condition 2) {condition 20% executes immediately code}...else  if (condition N) {condition n executes code}else{condition 1, 2 to n is not immediately executed code}

Suppose the math test, Xiao Ming exam 86 points, give him a review, 60 points below the failure, 60 (including 60 points) 75 is good, 75 (including 75)-85 is good, 85 (including 85)-100 excellent.

The code is represented as follows:

Results:

//================================================================================================

Multiple options (switch statement)

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

Grammar:

switch (expression) {case value 1:  Execute code block 1  break;case value 2: Execute code block  2  break;...case value N:  execute code block n  break;default:  code that executes when the case value is 1, 2...case value n is different.

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. If all case values do not match, the statement after default is executed.

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, and see the following code:

Execution Result:

Reviews: Keep trying! Reviews: Pass, come on! Reviews: Good, endeavor reviews: Excellent, very good reviews: Master, Daniel

In the above code, there is no break stop statement, if the score is 4, then the statement after case 5 will be executed, similarly, the statements after Case6, 7-10 will be executed.

//===================================================================================

Repeat repeat (for loop)

Many things do not just do it once, do it again. If you print 10 papers, print one copy at a time, and 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; loop condition; loop iteration) {   Looping statement}

If there are 6 balls in a box, we take one at a time and repeat the ball out of the box until the ball is finished.

<script type= "Text/javascript" >var num=1;for (num=1;num<=6;num++)//initialization value; loop condition; post-cycle condition value Update {  document.write ("Take out the first +num+" Ball <br/> ");} </script>

Results:

Implementation ideas:

//============================================================================

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) {    Loop statement}

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=0;  Initialize value while (num<=6)   //condition judgment {  document.write ("Remove" +num+ "ball <br/>");  num=num+1;  Conditional 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 executed first, after judging the condition, if the condition is true, continue the loop.

DO...WHILE Statement structure:

do{Loop Statement}while (judging condition)

We tried to output 5 numbers.

<script type= "Text/javascript" > num= 1;     do {document.write ("value:" + num+ "<br/>"); num++; Update condition} while (num<=5) </script>

Execution Result:

Why? Let's take a look at the implementation idea:

//===================================================================

Exit Loop break

Use the break statement in the while loop to exit the current loop and execute the following code directly.

The format is as follows:

for (initial conditions; condition; post-cycle conditional value update) {  if (special case)  {break;}  Loop code}

When a special situation occurs, the loop ends immediately. Take a look at the following example, output 10 numbers, and if the value is 5, stop the output.

Execution Result:

Note: The loop will end when num=5, and will not output the contents of the loop behind it.

//===============================================

Continue circulation continue

The function of the continue is to simply skip the cycle and the entire loop will continue to execute.

Statement structure:

for (initial conditions; condition; after cyclic condition value update) {  if (special case)  {continue;} Loop Code}

In the above loop, when a special case occurs, the loop will be skipped, and subsequent loops will not be affected. Like outputting 10 digits, if the number is 5, the output is not.

Execution Result:

Note: In the above code, the num=5 loop will be skipped.

//===================================================

JavaScript Learning Five

Related Article

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.