(7) JavaScript program control structure and statement------(2) Loop-controlled statements, jump statements, 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


while (conditional expression statement) {EXECUTE statement block}

When the return value of the conditional expression statement is true, the statement block in braces ' {} ' is executed, and when the statement block of the curly brace "{}" is executed, the return value of the conditional expression is detected again, and if the return value is true, the statement block in the curly brace "{}" is repeated until the return value is False , end the entire loop, and then execute the program code that follows the while code snippet.

Case: prompt five times when entering a webpage "Please call me five reputation."

<title> while statement using </title> var x=1;   while (x<6) {      alert ("Please call me five reputation brother" +x);      x + +;   //x = x + 1;  /** </script>        


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:

When the conditional expression statement is true, these are the blocks of code to loop through:

Statement 2;
Statement 3;
} while(conditional expression statement)

When the code executes from top to bottom, if it encounters a do, it executes the code in the curly brace "{}" of do once, executes it and then judges it through the while, and if the judgment passes then executes again, the loop is ended if the judgment does not pass.

{    var Username = prompt ("Please enter user name! ");    var password = prompt ("Please enter the password!") "); } While (username!= "Laoluo" | | password!= "222222"// that is, when the user name is not equal to "Laoluo" or when the password is not equal to "222222", the code in the do curly brace {} is executed again, according to the current "input" character again while judging ... Only the user name and password are correct to stop looping, and then execute other code. alert (' Login successful ');</script>        

That is, when the user name is not equal to "Laoluo" or when the password is not equal to "222222", the code in the do brace {} is executed again, judging by the current "input" character again while ...,
Only the user name and password are correct to stop the loop, and then the other code is executed: "Alert (' login succeeded ');"

》 》

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:

     Statement     //boolean judgment
For(initial conditions; judging condition; post-loop conditional value update) {EXECUTE statement block}


<script type= "Text/javascript" >
Contemporary? When executing from top to bottom, the code declares the variable y and assigns a value of 1, judging by the var y = 1 semicolon, judging if Y is less than 6, and executing the following loop code,
When the following code finishes executing, return to execution y++. This completes a cycle, the second cycle, you do not have to declare Y, just to determine whether Y is less than 6 on the line,
If satisfied, execute the following code, and then come back to y++.
for (var y=1;y<6;y++)

document.write ("<p style= ' font-size:" +y+ "0px ' > I most respect the person is Brother Wei! </p> ");

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 (completely ending the loop),The break statement is immediately out of the loop, that is, all subsequent loops are no longer executed.

syntax Format: Break ;

Use 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,} 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 function of the continue statement is to stop the loop that is being executed and go directly to the next loop. A popup dialog box (including a OK button and a Cancel button).

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


<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==3continue;} x=x + "The number is" + i + "<br>";} document.getElementById ("Demo"). innerhtml=x;} </script> </body>     

: This example skips the value 3.

Three, 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" dialog box

When visiting the website, sometimes suddenly pop up a small window, which is written with a text message. If you do not click "OK", you can not do anything to the Web page, this small window is the use of alert implementation.

The alert dialog box is used only for reminders, and alert pops up a message dialog box (with a OK button), but does not cause 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.


<title> Alert Alerts </title>  Function Ale ()      {// pop up a reminder dialog       alert ("Hello, byebye! ")      } </script> <p> Reminder dialog </p> <p>      <input type=" Submit "name=" Submit "value=" Submit "onclick=" ale () "/> </p> <p> is just a reminder that you can't make any changes to the script;</p> </body>    

(2) Confirm:

The Message dialog box, typically used to allow the user to make a selection action, pops up this dialog box containing a OK button and a Cancel button.


 <title> comfirm Reminders </title> function firm () {// Use the dialog box to return True or false if (Confirm (" Are you sure you want to go to the old Luo's home? " "// if True, Then turn the page to the old Luo Lake home location.href= "http://www.cnblogs.com/KTV123/" ;} else{alert (" Don't want to go? That would be hehe! "); The system returns False when the Cancel button is pressed. }</script><p> Confirmation dialog box </p> <p> <input type= "Submit" Name= "Submit2" value= "Submit" onclick= "Firm" ( ) "/> </p> <p> generally used to confirm information, return TRUE or false</p> </body>      

(3) Prompt:

Typically used to ask for information that needs to interact with the user, the popup dialog contains a OK button, a Cancel button, and a text input box.

The dialog box allows you to enter and return a string that is 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).


function Prom ()  {  var name=prompt ("Please enter your name", "");  assign the input to the variable name if  (name==)// If the returned contents are {alert ("Welcome:" +name);}
else} </script> <p> require user input, then give results </p> <p> <input type= "Submit" Name= "Submit3" value= "Submit" onclick= "Prom ()"/> </p> <p> a dialog box with input that returns a user-populated string </p> </body>

: Click "Submit"

The above content is from "JavaScript from scratch" and personal study notes, reproduced please specify the source! Thank you for your cooperation!

(7) JavaScript program control structure and statement------(2) Loop-controlled statements, jump statements, 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.