JavaScript Process Control Statements

Source: Internet
Author: User

1. If statement

1<script>2$ (document). Ready (function(){3$ ("#Button1"). Click (function(){4Comparenumber (5);5   })6   });7   functioncomparenumber (x)8   {9  if(x<10)Ten   { OneAlert ("x has a value of" +x+ "(less than 10)"); A  } -  Else -  { theAlert ("x has a value of" +x+ "(greater than or equal to 10)"); -  } -  } -</script>

2. Switch statement
Break is to exit the switch statement directly
Return is the statement that exits the function, which is also behind the switch statement block, and does not execute

1<script>2$ (document). Ready (function(){3$ ("#Button1"). Click (function(){4Numberrange (5);5 })6 });7 functionnumberrange (n)8 {9 varX=math.floor (N/10);//math.floor for roundingTen Switch(x) One { A  Case0: -Alert (the value of "N is" +n+ "(in the range of 0-9)"); - Braak; the  Case0: -Alert (the value of "N is" +n+ "(in the range of 10-19)"); - Braak; - default: +Alert ("N has a value of" +n+ "(not in the 0-9 range)"); - Braak; + } A } at</script>

3. For statement

1<script>2$ (document). Ready (function(){3$ ("#Button1"). Click (function(){4 varS=0;5  for(i=1;i<5;i++)6 {7S+=1;8 }9 alert (s);Ten }) One }); A</script>

4. For/in statement

1<script>2$ (document). Ready (function(){3$ ("#Button1"). Click (function(){4 output ();5 })6 });7 functionoutput ()8 {9 varS= "";Ten varM={x:10,y:20,z:30}; One  for(varVinchm) A { -s+= "Property name:" +v+ "; Property value:" +m[v]+ "\ n"; - } the alert (s); - } -</script>

5. While statement and Do-while statement

While statements are generally formatted as follows

 while (expression) {// statement sequence executed when expression is established }

The general format of the Do-while statement is as follows

 Do {// statement sequence }  while (expression)

The Do-while statement, which resumes execution of the next loop when the expression is formed. In this structure, the sequence of statements in the loop body is executed at least once.

While statement code example

1<script>2$ (document). Ready (function(){3$ ("#Button1"). Click (function(){4  varSum=0;5  varIndex=1;6   while(index<=10)7  {8sum+=index;9  }TenAlert ("1 to 10 and for:" +sum); One  }) A  }); -</script>

Do-while Statement Example

<script> $ (document). Ready (function() {$ ("#Button1"). Click (function  var sum=0var index=1 do {sum+=index*index;}  while (index<=10) alert ("The sum of squares between 1 and 10 is:"+ </script>

6. try-catch-finally statement

<script>$ (document). Ready (function(){$("#Button1"). Click (function(){intI=1 ; String Str= "Dfdfs"; Try{i= Convert.ToInt32 (str);//where there are anomalies.}  Catch(err) {i= 3;//Handling Exceptions}  finally{Console.WriteLine (i.ToString ()); }  })})</script>

JavaScript Process Control Statements

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.