5.1.3 Loop statements
Use loop statements to execute a code block repeatedly until the end of the loop is met. There are two loop statements in WML Script: for statement and while statement, and there are two operating languages that are closely related to loops: break statement and continue statement.
for statement
The for statement can create a conditional loop, which has 3 optional conditional expressions for controlling the loop. These 3 conditional expressions are placed in a bracket and are separated by semicolons (;). The general syntax form of a for statement is as follows:
for(initial expression; loop condition; incremental expression){
Code block
}
The execution of the for statement mainly includes the following steps:
(1) Execute the initial expression. In general, the function of the initial expression to assign initial values to the loop counter in a loop. So in this sense, the initial expression can also be defined in the form of "var variable declaration list;".
(2) Judge the cycle conditions. If the loop condition is true, then the statement in the loop body is executed, that is, to step (3); otherwise, if the loop condition is false or invalid, the loop ends;
(3) Execute loop code. Then, the incremental expression is executed. Generally, we process the loop counter in the incremental expression and finally execute it in return step 2.
For example, the following for statement creates a loop. The initial expression is to define the variable index and pay the initial value 0, the loop condition is index<100, and the incremental expression is to increase the index by 1 per loop. When the index increases to 100, the loop ends. The procedure is as follows:
for(var index=0;index<100;index++){
count+=index;
myFunc(count);
};
while statement
A common loop can also be used for a while statement, and its general syntax expression is as follows:
while(loop condition){
Code block
}
The execution process of a while statement includes the following steps:
(1) Determine whether the cycle conditions are true. If the loop condition is true, the loop is executed; if it is false or invalid, the loop is jumped out.
(2) Execute the code block in the loop and then return to step (1)
The following program is a simple example of using a while statement:
var counter=0
var total=0
while(counter<3){
counter++;
total+=c;
};
The established loop is executed only if the value of the variable counter is less than 3, otherwise the loop ends.
Obviously, if the loop condition cannot be false or invalid, then the while loop will be executed endlessly. Therefore, we must have variables in the code block that can change the loop conditions, otherwise, we may fall into a dead loop and cannot terminate the program. The following is an example of a dead loop:
var x=1;
var y=0;
while(x<=1){
y=x+1;
}
In this program, because the value of the variable x cannot change in the loop, the loop condition will always be true when judging, so it becomes a dead loop. Therefore, for while statements we often use the syntax form shown below:
Initial expression
while(loop condition){
Code block
Incremental expression
}
In this case, the functions of while statements are the same as those of for statements, but programs written in while statements are more readable. So we can also use the while statement to complete the loop where the index is increased to 100.
The procedure is as follows:
var index=0
while (index<100){
counter+=index;
myFunc(count);
index++;
};
Previous page123456789Next pageRead the full text