JavaScript Advanced (iii)

Source: Internet
Author: User

Now, say the judgment statement (IF)
The IF statement is the statement that is used when the code is executed based on the condition.
Grammar:
if (condition)
{Execute code when condition is set}
Note: If lowercase, uppercase letters (IF) will go wrong!
Suppose you apply for Web front-end technology development post, if you will HTML technology, your interview success, welcome to join the company
The code is represented as follows:
<script type= "Text/javascript" >
var mycarrer= "HTML";
if (Mycarrer = = "HTML")
{
document.write ("You are successful in the interview, welcome to join the company!" ");
}
</script>
So how do you do two of the options? It's if...else.
The If...else statement executes the code when the specified condition is true and executes the else code when the condition is not true
Grammar:
if (condition)
{Code executed when condition is set}
Else
{Code executed when the condition is not valid}
Suppose you apply for Web front-end technology development post, if you will HTML technology, your interview success, welcome to join the company
Otherwise your interview will not be successful and you cannot join the company.
The code demonstrates the following:
<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
{document.write ("Your interview is unsuccessful, you cannot join the company. ")}
</script>
To select a group to execute in multiple sets of statements, use the If...else nested statement.
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)
Suppose the math test, Xiao Ming exam 86 points, give him a review, 60 points below the failure, 60 (including 60)
-75 is divided into good, 75 (including 75)-85 for very good, 85 (including 85)-100 excellent.
The code is as follows:
<script type= "Text/javascript" >
var score=86;
if (score<60) {
Document.Write ("failed grades")
}else if (score<75) {
document.write ("good grades")
}else if (score<85) {
Doucment.write ("Good Score")
}else{
document.write ("good grades")
}
</scripe>
When there are many options, switch is more convenient than if else
Grammar:
switch (expression)
{
Case value 1;
Executing code block 1
Break
Case value 2;
Executing code block 2
Break
...
Case value N;
Execute code block n
Break
Dafault:
Code that executes when the case value is 1 and 2...case value n is different
}
Syntax Description:
Switch must assign an initial value, and the value matches each case value. Satisfies all statements after the case is executed and
Break statement to prevent the next case from running. If all case values do not match, the statement after default is executed.
Assuming the evaluation of the students test results, 10 points in full-scale system, we will be graded according to each grade, and root
According to the grade of grade to make different evaluation.
The code is as follows:
<script type= "Text/javascript" >
The var Myscore=6;//myscore variable stores the score, assuming 6
Switch (Myscore)//switch implementation, case 6 matches
{
Case 0:
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
Degree= "continue to work";
document.write ("Reviews" +degree+ "<br>");
Break
Case 6:
Degree= "Pass, come on! ";
document.write ("Reviews" +degree+ "<br>");
Break
Case 7:
Degree= "do, forge ahead!" ";
document.write ("Reviews" +degree+ "<br>");
Break
Case 8:
Degree= "Very good"
document.write ("Reviews" +degree+ "<br>");
Break
Case 9:
Case 10:
Degree= "Master, Daniel! ";
document.write ("Reviews" +degree+ "<br>");
}
</script>
Many things are not just done once, they need to be repeated. If you print 10 papers, print one copy at a time, repeat this
Action until the print is complete. These things, we use loop statements to complete, looping statements, is repeating
Execute a section of code.
For statement structure:
for (initialize variable; loop condition; loop iteration)
{
Looping statements
}
If there are six 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++)//Initialize value, loop condition, post-loop condition value Update
{
document.write ("Remove" +num+ "ball <br/>")
}
</script>
Here is a detailed explanation of the implementation of the ideas to help you understand:
First time:
Initialization: Num=1
Judgment: num<=6
Output: Remove the 1th ball
Condition Value Update: num++ (num=num+1,num value is 2)
Second time:
Judgment: Num<=6 (because the value of NUM after the first execution is 2, the 2<=6 is judged, the condition is true)
Output: Remove the 2nd ball
Condition Value Update: num++ (num=num+1,num value is 3)
Third time:
Judging: num<=6 (because the value of NUM after the second execution is 3, the 3<=6 is judged, the condition is true)
Output: Remove the 3rd Ball
Condition Value Update: num++ (num=num+1,num value is 4)
Fourth time, fifth time,//Middle
Sixth time: Num<=6 (because the value of NUM after the fifth execution is 6, judge the 6<=6, the condition is true)
Output: Remove the 6th Ball
Condition Value Update: num++ (num=num+1,num value is 7)
Seventh time: Num<=6 (because the value of Num after the sixth execution is 7, judging 7<=6, the condition is false, so no longer
Executes the output statement, ending the For loop. )
There is also a while loop that has the same functionality as the For loop, and the while loop executes a piece of code repeatedly until a
The condition is no longer satisfied.
While statement structure:
while (judging condition) {
Looping statements
}
Use the while loop to complete the action of taking the ball from the box, taking a total of 6 balls at a time.
<script type= "Text/javascript" >
var num=0;//initialization value
while (num<=6)//Condition judgment
{
document.write ("Remove" +num+ "ball <br/>");
num=num+1;//Condition Value Update
}
</script>
The basic principle of the do-while structure is essentially the same as the while structure, but it ensures that the loop body is at least
Line 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)
We tried to output 5 numbers.
<script type= "Text/javascript" >
Num=1;
Do
{
document.write ("Value:" +num+ "<br/>");
num++;//Update conditions
}
while (num<=5)
</script>
First time:
Output: value is 1
Condition Value Update: num++ (num=num+1,num value is 2)
Judge Num<=5 (num value is 2, Judge 2<=5, condition is true, do loop)
The second, third, and four are the same.
Fifth time:
Output: value is 5
Condition Value Update: num++ (num=num+1,num value is 6)
Judge Num<=5 (Num value is 6, so Judge 6<=5, condition is false, not loop)
PS: I admit I'm lazy. Just hit a big paragraph, the result because did not save. Oh da. You must remember.
Live in time to save this problem!!!
Use the break statement to exit the current loop in the while in Do...while while loop, directly following the
The code.
The format is as follows:
for (initial conditions; judging condition; post-cycle condition value update)
{
if (special case)
{break;}
Loop code
}
When a special situation occurs, the loop ends immediately. Take a look at the following example, output 10 numbers, if
A value of 5 stops the output.
<script type= "Text/javascript" >
var num;
for (num=1;num<=10;num++)
{
if (num==5)
{
break;//if NUM is 5, exit the loop
}
document.write ("Value" +num+ "<br/>")
}
</script>
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
}
In the above loop, when a special case occurs, the loop will be skipped, and the subsequent loop will not
be affected. Like outputting 10 digits, if the number is 5, the output is not.
<script type= "Text/javascript" >
var num;
for (num=1;num<=10;num++)
{
if (num==5)
{
Continue
}
document.write ("Value" +num+ "<br/>")
}
</script>
Note: In the above code, the num=5 loop will be skipped.
Do an exercise:
In a college programming elective class, we got a group of student data for the class, name, gender, age, and grade, and then we're going to use JavaScript to pick out the names of all the girls who are freshmen.
Student information is as follows: (' Small a ', ' female ', 21, ' Freshman '), (' small B ', ' Male ', 23, ' Junior '), (' Small c ', ' Male ', 24, ' Senior '), (' Small d ', ' female ', 21, ' Freshman '), (' small e ', ' female ', 22, ' Senior '), (' Little f ', ' male ', 21, ' Freshman '), (' Small g ', ' female ', 22, ' sophomore '), (' Little h ', ' female ', 20, ' Junior '), (' Small I ', ' female ', 20, ' Freshman '), (' Little J ', ' Male ', 20, ' Junior ')
I'll post the code in the comments.

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Process Control Statements </title>
<script type= "Text/javascript" >
var infos=[["Small A", "female", 21, "Freshman"],["small B", "Male", 23, "Junior"],["Small C", "male", 24, "Senior"],["Small D", "female", 21, "Freshman"],["small e", "female", 22, "Senior"],["small F" , "Male", 21, "Freshman"],["small g", "female", 22, "sophomore"],["small H", "female", 20, "Junior"],["small I", "female", 20, "Freshman"],["Little J", "Male", 20, "Junior"];
for (Var i=0;i<infos.length;i++)
{
if (infos[1]== "female" &&infos[3]== "freshman") {
document.write (infos[0]+ "<br/>")
}    }

</script>
<body>
</body>

JavaScript Advanced (iii)

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.