If...else statements
1 var a=12; 2 if (a>10) {3 alert ("A has a value greater than ten"); 4 }else{5 alert ("A value is not less than ten"); 6 }
If...else...if statements
1 var a=12; 2 if (a<) {3 alert ("A's value is less than ten"); 4 }else if (a==10) {5 alert ("A's value equals ten"); 6 }else{7 alert ("A value greater than ten"); 8 }
If nested statements
1 var a=8; 2 if (a>=10) { 3 if (a==10) { 4 alert ("a=10"); 5 }else{ 6 alert ("a>10"); 7 } 8}else{ 9 alert ("A's value is not more than ten"); Ten }
Switch...case statements
1 var a=2;2 switch (a) {3 Case 1:4 alert ("hem");5 Case 2:6 alert ("2222");7 Case 3:8 alert ("hehe");9 Case 4:Ten alert ("hehe"); One Default: A alert ("What Are you laughing at"); -}
1 var a=parseint (Prompt ("Please enter the first number");2 var b=parseint (Prompt ("Please enter a second number"));3 var c=prompt ("Please enter Operator");4 switch (c) {5 Case "+":6 alert (a+b);7 Case "-":8 alert (A-B);9 Case "*":Ten alert (a*b); One Case "/": A alert (A/b); - Case "%": - alert (a%b); the Default: - alert ("string Error"); -}
Looping statements
1 for (var a=0;a<a=a+1) {2 if (a==5) { 3 continue; 4 }5 alert (a); 6 }
The output is removed from 0-9 except 5.
Instance Application
1 //write out the odd number within 100 2 var str= ""; 3 for (Var a=1;a<a++) {4 if (a%2! =0) {5 str=str+a+ ","; 6 }7}8alert (str);
1 //Beg 1+2+3+4+ ... 10 and 2var sum=0; 3 for (Var a=1;a<one; a++) {4 sum=sum+a; 5 }6alert (sum);
1 //ask for factorial of 1-10 2 var sum=1; 3 for (Var a=1;a<one; a++) {4 sum=sum*a; 5 }6alert (sum);
1 //Output multiplication table 2 for (var a=1;a<, a++) {3for (var b=1;b<10;b+ +) {4 if (a<=b) {5 alert (A + "multiplied by" +b+ "equals" +a*b "); 6 }7 }8}
Can output 81 times multiplication formula
1 number of//100 within 7 related to 2 var str= ""; 3 for (Var a=1;a<a++) {4 if (a%7==0) | | (a%10==7) | | ((a-a%10)/10==7)) {5 str=str+a+ ","; 6 }7}8alert (str);
1 //Basketball from 5 meters height, each bounce height is 30% of the original height, output the sixth time to bounce height 2 var gao=5; 3 for (A=1;a<7; a++) {4 Gao=gao*0.3; 5 }6alert (GAO);
1 //Suppose a sesame weight 0.000001kg, to be filled with 32 squares of the chessboard, the first 1 tablets, the second 2 tablets, the third Geffon 4 tablets, fourth Geffon 8 tablets, filled with the need for multiple sesame. 2var sum=1; 3 var a=1; 4 for (Var i=1;i<; i++) {5 a=a*2; 6 sum =sum+a; 7 }8alert (sum*0.000001);
Nineth Lesson JavaScript Statement Application Example