Organize JavaScript Process Control statements Learn notes _javascript skills

Source: Internet
Author: User

1, make a judgment (if statement)
An IF statement is a statement that is used when the appropriate code is executed based on a condition.
Grammar:

 if (condition) {The
   code executes when the condition is set
 }

Example: Suppose you apply for Web front-end technology development post, if you will be HTML technology, your interview is successful, welcome to join the company.

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

2, two choose one (If...else statement)

The If...else statement executes code when the specified condition is set up, and executes the else code when the condition is not valid.
Grammar:

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

Example: If you apply for Web front-end technology development post, if you will be HTML technology, your interview is successful, welcome to join the company, otherwise your interview is not successful, can not join the company.

<script type= "Text/javascript" >
   var mycarrer = "html";//mycarrer variable storage 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>

3, multiple judgments (If...else nested statements)
to select a group of statements to perform, use the If ... else nested statement.
Grammar:

 if (condition 1)
 {condition 10% executed immediately by the code}
 else if (condition 2)
 {condition 20% code executed immediately}
 ...
 else if (condition N)
 {The code that executes when the condition n is set}
 else
 {conditions 1, 2 to n do not immediately execute code}

The United Nations World Health Organization (WHO) has a standard for age, under 44, as a youth, and 45 to 59 years old. Aged 60 to 89 and older than 90 years old. Zhaohong 99 years old this year, she belongs to which age stage.

<script type= "Text/javascript" >
   var myage =99;//Zhaohong Age is
   (myage<=44) {
     document.write ("Youth") ;
   } else if (myage<=59) {
     document.write ("middle-aged");
   } else if (myage<=89) {
     document.write ("old age");
   } else {
     document.write ("long-lived elderly");
   }
 </script>

4, a variety of options (switch statement)
When there are many options, the switch is easier to use than if else.

 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, and a value of 2...case value n is different
 

Syntax Description:
The switch must assign an initial value, and the value matches each case value. All statements that were executed after the case were satisfied, and the break statement was used to prevent the next case from running. If all case values do not match, execute the statement after default.
Example: We will do a week plan, 周一、二 learning concept knowledge, 周三、四 to enterprise practice, Friday experience, weeks Six Sunday rest and entertainment.

 <script type= "Text/javascript" >
   var myweek =3;//myweek represents the day of the week variable
   switch (myweek) {case
     1: Case
     2:
     document.write ("Learning concept knowledge");
     break;
     Case 3: Case
     4:
     document.write ("to Enterprise Practice");
     break;
     Case 5:
     document.write ("Lessons Learned");
     break;
     Default:
     document.write ("Week Six Sunday Rest and Entertainment");
   }
 </script>

5, repeat (for loop)
Many things do not just do it once, but do it again. If you print 10 papers, print one copy at a time, repeat this action until the print is complete. These things, we use the Loop statement to complete, the loop statement, is to repeat the execution of a piece of code.
For statement structure:

 for (initialization variable; loop condition; loop iteration)
 { 
   Circular statement 
 }

Example: 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; cyclic condition; after the loop condition value is updated
     document.write ("Take out the first" +num+ "Ball <br/>");
   }
 </script>

We have 1,2,3 ... 10 different denominations of money, use the For statement to complete the total, see how much money we have altogether?

 <script type= "Text/javascript" >
   var mymoney,sum=0;//mymoney variables are stored in different denominations, sum total for
   (Mymoney=1;mymoney <=10;mymoney++) { 
     sum= sum + mymoney;
   }
   document.write ("Sum total:" +sum);
 </script>

6, repeatedly (while loop)
There is a while loop that has the same functionality as the For loop, while the while loop repeats the code until a condition is no longer satisfied.
While statement structure:

 while (judging condition)
 {
   Circular statement
 }

Using the while loop, complete the action from the box to fetch the ball, one at a time, a total of 6 balls.

<script type= "Text/javascript" >
   var num=0;//initialization value while
   (num<=6) {//Conditional judgment
     document.write (" Remove the "+num+" Ball <br/> ");
     num=num+1; Conditional value Update
   }
 </script> 

7, to back and forth (Do...while cycle)
The basic principle of a 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 the first to execute the code, after judging the condition, if the condition is true, continue the loop.
DO...WHILE Statement structure:

 Do
 {
   loop statement
 } while
 (judging condition)

Try to output 5 digits.

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

Use the Do...while statement to output 6 digits.

<script type= "Text/javascript" >
   var mynum =6;//mynum initialization value is 6
   do{document.write
     ("Number: +mynum+" <br/> ");
     mynum=mynum-1;
   }
  while (mynum>=1);
 </script>

8. Exit Cycle Break
Use the break statement to exit the current loop in a while, a for, Do...while, and a while loop to execute the following code directly.
The format is as follows:

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

Test results output, if the results continue to output the next grade, if the results are not passed, exit and do not output after the result.

 <script type= "Text/javascript" >
   var mynum =new Array (70,80,66,90,50,100,89);//define array Mynum and assign value
   var i=0;
   while (i<mynum.length) {
     if (mynum[i]<60) {
     document.write ("Score" +mynum[i]+ "fail, do not cycle" + "<br>") ;
     break;
     document.write ("Achievement:" +mynum[i]+ "pass, continue circulation" + "<br>");
     i=i+1;
   }
 </script>

9. Continue circulation continue
Statement structure:

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

In the above loop, when special circumstances occur, this cycle is skipped, and subsequent loops are unaffected.
Example: Test results output, if the results continue to output the next grade, if the results fail, then do not output the result.

<script type= "Text/javascript" >
   var mynum =new Array (70,80,66,90,50,100,89);//define array Mynum and assign value
   var i;
   for (i=0;i<mynum.length;i++) {
     if (mynum[i]<60) {
       document.write ("fail, not output!") "+" <br> ");
       Continue;
     }
     document.write ("Score:" +mynum[i]+ "pass, Output!") + "<br>");
   }
 </script>

In a college programming elective class, we got a group of student data for the class, name, sex, age and grade, and then we use JavaScript to pick out the names of all the freshman girls.

Student information is as follows:

(' Little a ', ' female ', 21, ' Freshman '), (' Little b ', ' Male ', 23, ' Junior '),

(' small c ', ' Male ', 24, ' Senior '), (' Little d ', ' female ', 21, ' freshman '),

(' small e ', ' female ', 22, ' Senior '), (' Little f ', ' male ', 21, ' freshman '),

(' Little g ', ' female ', 22, ' sophomore '), (' Little h ', ' female ', 20, ' Junior '),

(' Small I ', ' female ', 20, ' Freshman '), (' Little J ', ' Male ', 20, ' Junior ')

<script type= "Text/javascript" >
  //The first step is to write the previous data in the form of an array that defines the variable as infos
  var infos = [
[' Little a ', ' female ', 21, ' freshman ']    ,
[' Little b ', ' Male ', 23, ' Junior '],
[' small c ', ' Male ', 24, ' senior '],
[' Little d ', ' female ', 21, ' freshman '],
[' e ', ' female ', 22, ' senior '],
[' Little f ', ' male ', 21, ' freshman '],
[' Little g ', ' female ', 22, ' sophomore '],
[' Little h ', ' Woman ', 20, ' Junior '],
[' Small I ', ' female ', 20, ' freshman '],
[' Little J ', ' Male ', 20, ' Junior ']
  ];
  First screening, find all the information of the freshman
  var arr1 = [];
  var n = 0;
  for (Var i=0;i<infos.length;i++) { 
    if (infos[i][3] = = "Freshman") { 
       arr1[n] = infos[i];
       document.write (arr1[n]+ "<br/>");
       N=n+1
    } 
  }
  document.write ("Number of freshmen:" +arr1.length+ "<br/>"); 
  Second screen, find all the girls ' information for
  (Var i=0;i<arr1.length;i++) { 
  //Here you can use switch 
    if (arr1[i][1]== ' female ') {
      document.write (arr1[i][0]+ "<br/>");
    }
</script>

The above is about JavaScript Process Control Statements of the example analysis, I hope to help you learn.

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.