In any language, program control flow is necessary, it can make the whole program reduce chaos, so that it smoothly in a certain way to execute. The following are commonly used program control flow structures and statements for JavaScript:
1, if condition statement
Basic format:
if (expression)
statement paragraph 1;
......
Else
statement paragraph 2;
.....
Function: If the expression is true, the statement segment 1 is executed, otherwise the statement segment 2 is executed.
Description: The If-else statement is the most basic control statement in JavaScript, which can change the statement's
The order of execution. In an expression, you must use a relational statement to implement the judgment as a Boolean value
to estimate. It converts 0 and nonzero numbers to false and true respectively. If the statement after if has more
Row, you must enclose it using curly braces.
The nested format of the IF statement:
if (Boolean) statement 1;
Else (Boolean) statement 2;
else if (Boolean) statement 3;
......
Else Statement 4;
In this case, each level of the Boolean expression will be computed, if true, then execute its corresponding statement, otherwise execute else after the statement. Now let's look at an example
Ifyuju.htm
<title>If 语句的小程序</title>
<script
language="JavaScript">
var activedate=new Date();
activehour=activedate.getHours()
if(activehour<12){
document.write("早上 好"+"<br>")
}else{
document.write("下午 好"+"<br>")
}
</script>
<body>
</body>
Description: This example uses the first defined variable activedate and the Newdate function to obtain the current time, however
After passing the variable activehour gets the current number of hours by comparing it with 12 to determine is "early
"or" Afternoon "then use the IF statement to execute the corresponding statement procedures, that is," Good Morning "or
"Good Afternoon".
2, for Loop statement
Basic format:
for (initialization; condition; increment)
Statement set;
Function: Implement conditional cycle, when the condition is set up, execute statement set, otherwise jump out of circulation body.
Note: Initialization parameters tell the starting position of the loop, the initial value of the variable must be given; condition is used to discriminate loops
The condition at which the stop occurs. If the condition satisfies, then executes the circulation body, otherwise jumps out. Increment: The main definition of follow
How the loop control variable changes in each loop. Between three primary statements, you must use a comma
Number separation
3, while loop
Basic format:
while (condition)
Statement set;
Note: This statement is the same as for statement, when the condition is true, repeat the loop, otherwise exit the loop. For and
While statements are both statements, using a For statement is easier to read when working with numbers.
is also more compact, while the while loop works more specifically for complex statements.
Xhyj11.htm
<title>循环语句事例</title>
<script language="JavaScript">
var sum=0
for(n=1;n<11;n++)
{
sum=sum+n
document.write(n,"
SUM=",sum,"<br>");
}
</script><body>
</body>
The same effect can be done with the while Loop statement, with the following code (or you can see the difference by looking at the source code):
Xhyj.htm
<title>循环语句事例</title>
<script language="JavaScript">
var sum,i;
i=1;
sum=0
while(i<=10){
sum+=i;
document.write(i,"
SUM=",sum,"<br>");
i++;
}
</script><body>
</body>