This article brings you an introduction to what process control is in Javascript (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Process control
JavaScript executes the program flow through process statements, and the program flow consists of several statements. Under normal circumstances, the statements in the program are executed in the order they are written. This structure is called a sequential structure. In addition to sequential structures there are selection structures and loop structures.
1. Selection structure
(1) if-else statement
Syntax:
if(condition){ statementS; }else{ statements; }
(2) if-else if-else statement
Grammar:
if(condition){ statementS; }else if{ statements; } …… else{ statements; }
For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta charset="GB2312"> <title>选择结构</title> <script language="JavaScript"> function max() { var fist=parseInt(form1.fist.value); var secod=parseInt(form1.secod.value); if(isNaN(fist)){ alert("第一个数不是数值类型"); form1.fist.value=""; } else if (isNaN(secod)) { alert("第二个数不是数值类型"); form1.secod.value=""; }else{ var max=(fist>=secod?fist:secod); document.write("两个数之间较大的数为:"+max); } } </script> </head> <body> <form name="form1"> 请输入第一个数(数值型): <input type="text" name="fist" /> <br> 请输入第二个数(数值型): <input type="text" name="secod" /> <br> <input type="button" ONCLICK="max()" value="选择较大值" /> <input type="reset" value="重填" /> </form> </body> </html>
(3) switch statement (optional in [ ])
Syntax:
switch(expression){ case value1: statement; break; case value2: statement2; break; …… case valueN; statementN; break; [defalut: defalutStatements;] }
For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"" <html> <head> <meta charset="GB2312"> <title>switch语句</title> </head> <body> <script language="JavaScript"> var day= new Date().getDay(); switch (day){ case 0: title="今天是星期日"; break; case 1: title="今天是星期一"; break; case 2: title="今天是星期二"; break; case 3: title="今天是星期三"; break; case 4: title="今天是星期四"; break; case 5: title="今天是星期五"; break; case 6: title="今天是星期六"; break; } document.write(title); </script> </body> </html>
2. Loop statement
(1) for loop (a loop with a known number of loops)
Syntax:
for(inintal-condition;conditin;increment){ statements; }
Example:
Output the multiplication table
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>循环结构</title> </head> <body> <font size="-1" color="blue"> <script language="JavaScript"> var i,j; for(i=1;i<=10;i++){ for(j=1;j<=i;j++){ document.write(j+"*"+i+"="+i*j); document.write(" "); } document.write("<br>"); } </script> </font> </body> </html>
(2) for-in statement (usually used Traverse the array)
Grammar:
for(elements in object){ statement; }
For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="GB2312"> <title>for-in</title> </head> <body> <script language="JavaScript"> var student=new Object(); student.name="王明明"; student.no="20120156"; student.addreess="山东济南"; for(e in student){ document.write(e+":"+student[e]+"<br>"); } </script> </body> </html>
(3) while statement (unknown number of loops)
Grammar:
while(condition){ statement; }
For example:
Output multiples of 3 in 1-100
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="GB2312"> <title>while循环</title> </head> <body> <script language="JavaScript"> var i=1; while(i<100){ if(i%3==0){ document.write(i+" "); } i++; } </script> </body> </html>
(4) do-while statement (this statement is executed at least once)
Syntax :
do{ statement; } while(conditions);
For example:
Calculate the sum of 1-100
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="GB2312"> <title>do-while语句</title> </head> <body> <script language="JavaScript"> var i = 1; var num = 0; do { sum+=i; i++ }while(i<=100); document.write("1-100的和是:"+sum); </script> </body> </html>
Transfer statement
(1) break statement
The break statement is used in loop statements such as switch statements and for statements. . Used to terminate the switch statement and execute the statement after the switch statement.
(2) continue statement
The continue statement is used in for, while, do-while, and for-in statements to end this loop and execute the next loop. It is generally used with if statements are used together.
(3) return statement
The return statement is generally used in functions. You can use the return statement expression format to return any type of function value. The returned function value can be accepted by a variable.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!
The above is the detailed content of What is flow control in Javascript (code example). For more information, please follow other related articles on the PHP Chinese website!