Basic JavaScript flow control statement usage, javascript statement

Source: Internet
Author: User
Tags case statement

Basic JavaScript flow control statement usage, javascript statement

Part 3: Process Control statements

JavaScript code is the writing position:

JavaScript code should be written in <script type = "text/javascript"> </script>.

Or as an external reference <script src = "JavaScript code path"> </script>

Use a semicolon to end each JavaScript sentence.

Output statement

1. console output: console. log ();

It can output some information in the console, and the output information is the content of parentheses in console. log.

This statement is often used during program debugging.

2. Output in the pop-up box:

Alert ();

You can use alert to display information in parentheses of alert () in a prompt box on the webpage.

Prompt ();

You can use prompt to pop up an input box on the webpage and use the information in parentheses of prompt () as the prompt information.

Confirm ();

The dialog box that contains confirmation and cancellation is displayed.

3. Page output: document. write ();

The content is directly displayed on the page.

Select statement

If... else statement

If (Judgment condition/boolean value ){
// If conditions are met, the following code is executed.

Code 1;

} Else {
// When the preceding conditions are not met, or the boolean value is false, the following code 2 is executed.

Code 2

}

In addition, after else, you can continue to add if to judge

If (Judgment condition/boolean value ){
// If conditions are met, the following code is executed.

Code 1;

} Else if (Judgment condition ){
// When the preceding conditions are not met, or the boolean value is false, the following code 2 is executed.

Code 2

}

Else if (condition)

.....

Else {
Final code

}

Switch... case statement

Used to determine multiple possible values

The switch statement is most closely related to the if statement, and is also a flow control statement widely used in other languages.

switch (expression) {case value: statementbreak;case value: statementbreak;case value: statementbreak;case value: statementbreak;default: statement}

The meaning of case in a switch statement is: "If the expression is equal to this value, execute the following statement (statement )". The break keyword causes the code execution flow to jump out of the switch statement. If the break keyword is omitted, the next case will be executed after the current case is executed. By adding a break statement after each case, you can avoid executing multiple case codes at the same time.

It can also be mixed in multiple situations.

Switch (I) {case 25:/* merge two cases */case 35: alert ("25 or 35"); break; case 45: alert ("45 "); break; default: alert ("Other ");}

Note that the switch statement uses full operators when comparing values, so no type conversion occurs (for example,
String "10" is not equal to the value 10 ).

BreakAndContinueStatement

Break ends the pass loop within the loop.

Continue ends the cycle within the cycle and starts the next cycle:

Loop statement

ForLoop is to repeatedly execute the same piece of code.

For (var I = 1; judgment condition; I ++ ){
Code block to be recycled:

}

When the program runs for, it first declares a variable I and assigns a value of 1 to determine whether I meets the following judgment conditions. If yes, execute the following code block to be recycled, after the code block is executed, execute I ++ to determine whether the conditions are met. If the conditions are met, execute the for loop again according to the preceding process. If the conditions are not met, end the for loop.

The for loop can also be used for nesting to implement complex operations. The nested for loop is used for Bubble sorting. The following are two examples of for loop nesting.

Print right triangle,

for (var i = 1; i <= 10; i++) {for (var j = 1; j <= i; j++){document.write("☆");}document.write("<br/>");}

Print 99 multiplication table

for (var i = 1; i < 10; i++) {for (var j = 1; j <= i; j++) {document.write(j + "*" + i + "=" + i * j);// 1 * 1 = 1document.write(" ");}document.write("<br/>");}

For in Loop

For-in is used to traverse the attributes of an array or object (cyclically operate the attributes of an array or object ).

For example

var xvar mycars = new Array()mycars[0] = "Saab"mycars[1] = "Volvo"mycars[2] = "BMW"for (x in mycars){document.write(mycars[x] + "<br />")}

While Loop

While (Judgment condition/boolean ){
Code block

}

When the code is executed to while, it first determines whether the condition is true. If it is true, the code block in the while braces will be executed. After the code block is executed, return to while again and then judge. If it is true, execute the code block in the while braces again, and return the while again. If it is false, it will not be executed.

Note: in the future, when writing code, you must note that the judgment condition of the loop cannot always be true, otherwise it will become an endless loop.

Do... while loop

The do-while statement is a post-test loop statement that tests exit conditions only after the code in the loop body is executed.
In other words, the code in the loop body is executed at least once before the conditional expression is evaluated.

do {statement} while (expression);

Do... while statements are not used in development. The most commonly used is for loop and for loop nesting.

Supplement:

Function object

Function declarative

Function fn () {// function body}

Function expression (anonymous function)

Var fn = function () {// function body}

// Fn indicates the function name

// The function expression is usually called an anonymous function because there is no function name.

Function call

Fn (); // Note: The function will not be executed unless it is called.

Function Parameters

// Statement

Function Name (parameter 1, parameter 2, parameter 3 ,){

// Function body

}

// Call

Function Name (real parameter 1, real parameter 2, real parameter 3 );

Function Name (real parameter 1); // no problem in writing this way

Function Name (real parameter 1, real parameter 2, real parameter 3, real parameter 4); // no problem in writing this way

// Note: the number of real parameters of a function can be different from that of a form parameter.

Function return value

Function Name (){

Return: return value;

}

// Note: When the function does not write the return value, the default return value is undefined.

Recursion of functions: A function internally calls its own function called recursion.

function fn(){fn();}fn();

Function callback: The function passed as a parameter is called a callback function.

Function fn1 () {console. log ("I Am a callback function");} function fn2 (parameter) {parameter (); // call the function // The parameter here represents the passed function fn1} fn2 (fn1); // fn1 is a callback function

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.