Notes for JavaScript Process Control statements

Source: Internet
Author: User

Process Control statements are critical in JS. The Process Control statements in JS include 7 kinds of statements, such as if condition judgment statement, for Loop statement, while Loop statement, Do......while Loop statement, break statement, continue statement and switch statement.

One, if statement

The IF statement is the most basic conditional judgment statement. Performs the appropriate processing according to the expression criteria. The syntax example is as follows:

  code is as follows copy code
     VAR a=5;           //Declare variable A, and assign a value of 5.
    var b;             // Declare variable B.
    if (a==b) {           //judgment, if a=b, Then execute
       alert ("b=5");   //window, b=5.
   }

1.a==b is the condition in the IF statement.
2.alert ("A=c") is the result in the IF statement.
The meaning of this script is to declare the variable A, B, and assign a value of a=5; When a=b, the window b=5.
Note: Curly braces in this script can be omitted.

Second, if ... else statement

A if...else statement is a standard form of an if statement. Adding else on the basis of if will make the statement more complete.
The syntax for this statement is as follows:

The code is as follows Copy Code
var a=5;
var b;
if (a==b) {
Alert ("b=5");
}else {//If a!=b executes the following
Alert ("b!=5");

The meaning of this script is to declare the variable A, B, and assign a value of a=5; When a=b, the window b=5, or the window b!=5.

Third, If...else if statement www.111cn.net

The If...else if statement is a flexible application of an if statement.
The syntax for this statement is as follows:

The code is as follows Copy Code
var a=5;
var b;
var C;
var D;
var e;
if (a==b) {
Alert ("B=5")
}else if (a==c) {
Alert ("C=5")
}else if (a==d) {
Alert ("d=5")
}else if (a==e) {
Alert ("E=5")
}else{
Alert ("B!=5, c!=5, d!=5, e!=5")
}

Grammatical meaning: Ming variable A, B, C, D, E, and assign value a=5; When the a=b, the window b=5, when the a=c, the window c=5, when the A=d, the window d=5, when the a=e, the window e=5, or window b!=5, c!=5, d!=5, e!=5.

Nested use of IF statements

If statements can be nested within an if statement, we can use {} to differentiate between external if and internal if. If you do not use {} to determine the layer relationship, it may cause the output of the program code is completely different from the ideal meaning.
The syntax example is as follows:

The code is as follows Copy Code
<script type= "Text/javascript" >//Script start
var m=12;n=m; Declare variables n, m values are 12
if (m<1) {//outer if, to determine when m<1 execute the following
if (n==1)//inner layer, when n equals 1 o'clock output below
Alert ("When M is less than 1 o'clock, n equals 1"); window, when M is less than 1 o'clock, n equals 1
else//when n is not equal to 1 o'clock, perform the following
Alert ("When M is less than 1 o'clock, n is not equal to 1"); window, when m not less than 1 o'clock, n is not equal to 1
}else if (m>10) {//outer else if, to perform the following when judging the outer m>10
if (n==1)//inner layer, if n equals 1, perform the following
Alert ("When M is greater than 10 o'clock, N equals 1"); window, when M is greater than 10 o'clock, n equals 1
else//n not equal to 1 o'clock, perform the following
Alert ("When M is greater than 10 o'clock, n is not equal to 1"); window, when M is greater than 10 o'clock, n is not equal to 1
}
</script>//End of script

Here the use of the IF statement is basically finished, the above syntax examples are the most basic simple, below gives you a use if statement to determine whether to enter the user name example.
HTML section:

JS section:

Conclusion: This paper mainly introduces the IF statement in the process Control statement, including the multiple usages and examples of IF. These are all relatively simple basic applications.
In subsequent articles, it will increase the length of the core technology. I hope you will support us a lot.

Three. Switch Statement www.111cn.net

The code is as follows Copy Code

var box = 1;

Switch (box) {

Case 1:

Alert (' one ');

Break

Case 2:

Alert (' two ');

Break

Default

Alert (' Error ');

Four. Do......while statement

is a first run, after the judgement of the loop statement, at least first run the loop body.

Five. While statement

First judged, after running, must meet the conditions before it can run

Six. For statement

Judge first, then run, have the execute code after initializing the variable and defining the loop before executing the loop

Seven. for......in

The code is as follows Copy Code

var box ={

' Name ' = ' Caibaojian ',

' Age ' = 24

}

for (var x in box) {

alert (x);

}

Nine. With statement

  code is as follows copy code

var box = {

' name ' = ' Caibaojian ',

' age ' = #

}

with (box) {//with (box) can give box to omit

Var n= name;

P>var a = age;

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.