One, if statement
Just like Chinese translation, if ... Then ....
<script type= "Text/javascript" >// Only two cases if(condition) { The block of statements to execute; } Else { The block of statements to execute; } // In many cases if (condition) { The block of statements to execute; } Else if (condition) { The block of statements to execute; } Else { The block of statements to execute; } </script>
Common Format
If (this condition is met) {execute this statement}
<!doctype html>if, Swith) </title><script type= "Text/javascript" >varA=prompt (); if(a==5) {alert ("A=5"); } Else{alert ("A!==5"); }//There are only two cases if(a<5) {alert ("A<5"); } Else if(a>=5&&a<=10) {alert ("A>=5 and a<=10"); } Else if(a>10&&a<20) {alert ("A>10 and A<20"); } Else{alert ("A is not in range"); } </script>Example 1<!doctype html>varA=prompt (); if(a>=0&&a<=100) {alert ("A∈[0,100]"); } Else{alert (A? [0,100] "); } varb=prompt (); if(b<=50&&b>=0) {alert ("Hard work, don't be lazy!" "); } Else if(b>=50&&b<=60) {alert ("Just a little bit more."); } Else if(b>=80&&b<=100) {alert ("You've learned well, you deserve praise."); } Else if(b>=60&&b<=80) {alert ("You've made a lot of progress."); } Else{alert ("100"); } varManheight=prompt ("Please enter a man's height"); varmanweight=prompt (); if(manweight-manheight+100>=-3&&manweight-manheight+100<=3) {alert ("Your weight is standard"); } Else{alert ("You need a healthier lifestyle."); } varwomenheight=prompt (); varwomenweight=prompt (); if(womenweight-womenheight+110>=3&&manweight-manheight+110<=3) {alert ("Your weight is standard"); } Else{alert ("You need a healthier lifestyle."); }</script>Example 2 varA=parsefloat (Prompt ("Please enter a two-time equation two power factor A (a not equal to 0)")); varB=parsefloat (Prompt ("Please enter one Yuan two times the power Factor B")); varC=parsefloat (Prompt ("Please enter a unary two-time equation constant")); if("(b*b)-(4a*c) >0") {document.write ("The second equation has two real roots"); } Else if("(b*b)-(4a*c) ==0") {document.write ("The second equation has only one single"); } Else if("(b*b)-(4a*c) <0") {document.write ("Quadratic equations have no real roots"); } Else{document.write ("Please enter the correct value"); }
One-dollar two-time equation withSecond, switch
<script type= "Text/javascript" > switch(expression) { case value 1: statement block executed: break ; Case Value 2: block of statements executed:break; Case Value 3: block of statements executed:break; ... default : The block of statements executed; } </script>
Csae value: This value must be the same type as the value of the selected place, the value is not default to the string type, when entering a number is the default numeric type
varA=prompt ("Please enter today's date")); varb=parseint (a); Switch(b) { Case2.26: Alert ("Today is Monday"); Break; Case2.27: Alert ("Today is Tuesday"); Break; Case2.28: Alert ("Today is Wednesday"); Break; Case3.1: Alert ("Today is Thursday"); Break; Case3.2: Alert ("Today is Friday"); Break; Case3.3: Alert ("Today is Starday"); Break; Case3.4: Alert ("Today is Sunday"); Break; }
Example 1<!doctype html>//Date vard1=NewDate ();//the first method of creating a son//document.write (d1.tostring () + "<br>"); //second method of creation//var d2=new Date ("2009-02-28 18:18:18");//document.write (d2.todatestring ());D1.setfullyear (2018); D1.setmonth (0); D1.setdate (18); varYear=d1.getfullyear (); varMonth=d1.getmonth (+1); varDate=d1.getdate (); varday=D1.getday (); Switch(day) { Case0: Day= "Sunday"; Break; Case1: Day= "Monday"; Break; Case2: Day= "Tuesday"; Break; Case3: Day= "Wednesday"; Break; Case4: Day= "Thursday"; Break; Case5: Day= "Friday"; Break; Case6: Day= "Saturday"; Break; default: Day= "Error Data"} document.write ( year+ "Year" +month+ "month" +date+ "Day" +Day ); </script>Get Time<!doctype html>functionTest () {varA=document.getelementbyid (' name '). Value; varB=document.getelementbyid (' Sex '). Value; varC=document.getelementbyid (' Hello '); vare=/\w/; varRes=e.test (a);//Regular expressions to keep up with a judgment or return if(a== "") {alert ("You must enter a name to eject"); }//Else if (!isnan (a)) {//alert ("must enter non-numeric to be allowed");// } Else if(res==true) {alert ("Please enter the word character"); } Else if(a!== ""){ Switch(b) { Case"S"://csae Value: This value must be the same type as the value of the selected place, the value is not default to the string type, when entering a number is the default numeric typeAlert (A + "Hello"); Break; Case"Man": Alert (a+ "Hello, sir"); Break; Case"Woman": Alert (a+ "Hello Ladies"); Break; default: Alert ("Error Data"); } }} </script>Example II-Greetings<script type= "Text/javascript" >
There are only two cases in which
if (condition) {
The block of statements to execute ;
}else{
The block of statements to execute ;
}
In many cases
if (condition) {
The block of statements to execute ;
}else if (condition){
The block of statements to execute ;
}else{
The block of statements to execute ;
}
</script>
JS Basic 5-Conditions of the flow control statement (if, switch)