JavaScript program control structure and statement------(2) Loop-controlled statements, jump statements, dialog boxes

Source: Internet
Author: User

Tag: equals for Loop statement Boolean nbsp WWW code click Technology Share MIT

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 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> 

》》》》》》

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:
bo=

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.

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}

Case:

<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> ");
}
</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 (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:

<body>    <p> Click the button to test the loop with the 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 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.

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)        {        continue;        }      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.

Case:

<title> Alert Reminders </title> 

(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.

Case:

<title> comfirm Reminders </title> 

(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).

Case:

<title> prompt Tips </title>    Else  } </script> <p> require user input, then give a result </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 above content is from "JavaScript from scratch" and personal study notes, reproduced please specify the source! Thank you for your cooperation!

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.