(7) JavaScript programming structure and statements------(2) loop control statements, jump statements, using dialog boxes

Source: Internet
Author: User

First, the loop control statement

The loop statement mainly consists of executing an operation repeatedly while satisfying the condition, and the loop control statement mainly includes the while statement , thedo...while statement and the FOR statement .

1. While statement

Grammar:

while (conditional expression statement) {EXECUTE statement block}

when the return value of the conditional expression statement is true, the statement block in the curly brace "{}" is executed, and when the statement block of the curly brace "{}" is executed, the return value of the conditional expression is again detected, and if the return value is true, the statement block in the curly brace "{}" is executed repeatedly. Until the return value is false, the entire loop is ended, and then the program code after the while code snippet is executed down.

Case: computes The sum of all integers of the 1~100

<title> while statement </title>  var x=0;   var xday=0;    while (x<=100)  {  Xday+ =x;  X+ +;   //The loop should contain statements that make the loop exit, such as X + + of the case above (otherwise the loop will run endlessly)  }  document.write ("sum of all integers of 1~100" +xday);   </script> </body>

2. Do...while statement:

It is true that the value of a conditional expression statement is not detected until the statement in the curly braces is executed first.

 Do ... while statement syntax: do{statement 1;
Statement 2;
Statement 3;} while(conditional expression statement)

Case:

do ... while statement </title>  var x=0;   var xday=0;    Do {  XSum+ =x;  X+ +;  }  while (x<=100);  document.write ("sum of all numbers of 1~100" +</script> </body>

3. For Loop statement

A For statement is usually made up of two parts: a conditional control section and a loop part in another part.

For Loop statement syntax:

 for (initialize expression; loop conditional expression; loop action expression) {EXECUTE statement block}

Case:

<script type= "Text/javascript" > For  (var x=0;x<10;x++)  {  document.write ("<p style= ' font-size:" +x+ "0px ' > Welcome to Learning javascript</p>")  ; </script>

Second, jump statement

The jump statements supported by JavaScript are mainly continue statements and break statements.

1. Break Statement :

Used to jump out of the loop (to completely end the loop),after the break statement jumps out of the loop, the code after the loop continues to execute, if any.

Syntax: Break ;

Case:

     The loop of the <body> break statement. </p>    <button onclick= "myFunction ()" > click here </button>    <p id= "Demo" ></p>    <script>    function  myFunction ()    {    var x= "", i=0;      for (i=0;i<10;i++)      {      if (i==3)        {        break;        }      X=x + "The number is" + i + "<br>";      }    document.getElementById ("Demo"). innerhtml=x;    }     </script> </body>

2. Continue statement

The continue statement is similar to the break statement, except that the continue statement is used to abort the loop and start the next loop .

Syntax:  continue;

The continue statement can only be used in the while, for, do...while, switch statements.

Case:

 <body><p> Click the button below to execute the loop, which skips the i=3 step.    </p> <button onclick= "myFunction ()" > click here </button> <p id= "Demo" ></p> <script> function   MyFunction () { var  x= "", I=0;  for  (I=0;i<10;i++ if  (I==3 cont        Inue  ;      } x  =x + "The number is" + i + "<br>" ;    } document.getElementById ( "Demo"). Innerhtml=X; }  </script> </body> 

: This example skips the value 3.

Third, use the dialog box

There are three styles of dialog boxes in JavaScript that can be used as hints, OK and input, corresponding to three functions: alert, confirm, prompt.

(1) Alert: This dialog box is for reminders only and cannot have any changes to the script. It has only one parameter, which is the information that needs to be prompted, and there is no return value.

Case:

<title> Alert Alerts </title>  function  Ale ()      {// pop up a reminder dialog       alert ("Hello, byebye! ")      </script> <p> Demo: Reminder dialog </p> <p>      <input type=" Submit "name=" Submit " Value= "Submit" onclick= "ale ()"/> </p> <p> is just a reminder that you cannot make any changes to the script;</p> </body>

(2) Confirm : This dialog box is typically used to confirm information. It has only one parameter, and the return value is true or false.

Case:

<title> comfirm Reminders </title> functionfirm () {//returns TRUE or false using the dialog box       if(Confirm ("Are you sure you want to turn to Lao Luo's home?") "))          {//If it is true, then turn the page to the old Luo Lake homelocation.href= "http://www.cnblogs.com/KTV123/"; }          Else{alert ("The system returns false after the < cancel > button is pressed"); }      }</script><p> Demo: confirmation dialog box </p> <p> <input type= "Submit" Name= "Submit2" value= "Submit" onclick= "Fir M () "/> </p> <p> generally used to confirm, return TRUE or false</p> </body>

: Click "Submit"

(3) Prompt: This dialog box can be entered and returns the string entered by the user. It has two parameters, the first parameter displays a hint, and the second parameter displays the input box (and the default value).

Case:

function Prom ()  {  var name=prompt ("Please enter your name", ""); // assign the input to the variable name  if (name) // If the returned content is       {         alert ("welcome you:" +name);      }   </script> <p> Demo: Require user input, then give results </p>  <p>    <input type= "Submit" Name= "Submit3" Value= "Submit" onclick= "Prom ()"/>   </p> <p> A dialog box with input to return the user-populated string </p> </body>

: Click "Submit"

/* The content section above comes from "JavaScript starts from scratch", "W3school offline manual Simplified Chinese version" and individual study notes, this article final explanation right belongs to oneself all! */

(7) JavaScript programming structure and statements------(2) loop control statements, jump statements, using dialog boxes

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.