JavaScript Process Control Statements

Source: Internet
Author: User

One, if statement

The IF statement is the statement that is used when the code is executed based on the condition.

Syntax: if (conditional)

{Execute code when condition is set}

Example:

<script type= "Text/javascript" >
var mycarrer = "HTML";
if (Mycarrer = = "HTML")
{
document.write ("You are successful in the interview, welcome to join the company. ");
}
</script>

Second, If...else statement (two choice one)

The If...else statement executes the code when the specified condition is true and executes the else code when the condition is not established.

Grammar:

if (condition)
{Code executed when condition is set}
Else
{Code executed when the condition is not valid}

Example:

<script type= "Text/javascript" >
var mycarrer = "HTML"; Mycarrer Variable Storage Skills
if (Mycarrer = = "HTML")
{document.write ("You are successful in the interview, welcome to join the company. "); }
else//Otherwise, the skill is not HTML
{document.write ("Your interview is unsuccessful, you cannot join the company. ");}
</script>

Three, If...else nested statements (multiple judgments)

To select a group to execute in multiple sets of statements, use the IF: else nested statements.

Grammar:

if (condition 1)
{Condition 10% code to execute immediately}
else if (condition 2)
{Condition 20% code to execute immediately}
...
else if (condition N)
{The code executed when condition n was established}
Else
{Conditions 1, 2 to n are not immediately executed code}

Example:

<script type= "Text/javascript" >
var myage =9;//Yangwen Age is 99
if (myage<=44)
{document.write ("Youth");}
else if (myage<=59)
{document.write ("middle-aged");}
else if (myage<=89)
{document.write ("old Person");}
Else
{document.write ("Longevity Elder");}
</script>

Iv. switch statements (multiple choices)

Switch is more convenient than if else when there are a number of options.

Grammar:

switch (expression)
{
Case value 1://Note After the value must be written ":"
Executing code block 1
Break
Case Value 2:
Executing code block 2
Break
...
Case Value N:
Execute code block n
Break
Default
Code that executes when the case value is 1 and 2...case value n is different
}

Note: Switch must assign an initial value, and the value matches each case value. Satisfies all statements after the case is executed and uses the break statement to prevent the next case from running. If all case values do not match, the statement after default is executed.

Example:

<script type= "Text/javascript" >
Var Myweek =3;//myweek represents the day of the week variable
Switch (Myweek)
{
Case 1:
Case 2:
document.write ("Learning concept knowledge");
Break
Case 3:
Case 4:
document.write ("to Enterprise Practice");
Break
Case 5:
document.write ("Lessons Learned");
Break
Default
document.write ("Week Six, Rest and entertainment");
}
</script>

V. For loop (repeat repeat)

Many things do not just do it once, do it again. If you print 10 papers, print one copy at a time, and repeat the action until the print is complete. These things, we use loop statements to complete, looping statements, is to repeat the execution of a piece of code.

Grammar:

for (initialize variable; loop condition; loop iteration)
{
Looping statements
}

Example: If there are 6 balls in a box, we take one at a time and repeat the ball out of the box until the ball is finished.

<script type= "Text/javascript" >
var num=1;
For (num=1;num<=6;num++) //initialization value; loop condition; post-cycle Condition value Update
{document.write ("Remove" +num+ "ball <br/>");
}
</script>

Vi. while loop (over and over)

There is also a while loop that has the same functionality as the For loop, and the while loop repeats a piece of code until a condition is no longer satisfied.

While statement structure:

while (judging condition)
{
Looping statements
}

Example: Using a while loop, complete the action of taking the ball from the box, taking one at a time, a total of 6 balls.

<script type= "Text/javascript" >
var num=0; Initialize value
while (num<=6)//Condition judgment
{
document.write ("Take out the first +num+" <br/> ");
num=num+1; Condition Value Update
}
</script>

Vii. do...while statements (to and fro)

The basic principle of the do-while structure is essentially the same as the while structure, but it guarantees that the loop body is executed at least once . Because it is executed first, after judging the condition, if the condition is true, continue the loop .

DO...WHILE Statement Structure

Do
{
Looping statements
}
while (judging condition)

Example: We try to output 5 numbers.

<script type= "Text/javascript" >
Num= 1;
Do
{
document.write ("Value:" + num+ "<br/>");
num++; Update condition
}
while (num<=5)
</script>

Eight, break statement (exit Loop)

Use the break statement in the while loop to exit the current loop and execute the following code directly.

Syntax structure:

for (initial conditions; judging condition; post-cycle condition value update)
{
if (special case)
{break;}
Loop code
}

Example:

<script type= "Text/javascript" >
var mynum =new Array (70,80,66,90,50,100,89);//define array Mynum and assign values
var i=0;
while (I<mynum.length)
{
if (mynum[i]<60)
{break;
document.write ("Score" +mynum[i]+ "failed, do not cycle the" + "<br>");

}
document.write ("Score:" +mynum[i]+ "pass, continue circulation" + "<br>");
i=i+1;

}
</script>

Nine, continue continue to circulate

The function of the continue is to simply skip the cycle and the entire loop will continue to execute.

Statement structure:

for (initial conditions; judging condition; post-cycle condition value update)
{
if (special case)
{continue;}
Loop code
}

Example:

Script type= "Text/javascript" >
var mynum =new Array (70,80,66,90,50,100,89);//define array Mynum and assign values
var i;
for (i=0;i<mynum.length;i++)
{
if (mynum[i]<60)
{
document.write ("Failure of grades, not output!") "+" <br> ");
Continue
}
document.write ("Score:" +mynum[i]+ "pass, Output!") + "<br>");
}
</script>

This article refers to the MU class network

JavaScript Process Control Statements

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.