SoFunction
Updated on 2025-04-14

WMLScript Script Programming Page 6/9


break statement
In order to better solve the problem of dead loop, most WML Script items provide break statements in the same way as in the compilation language. The break statement can cause the program execution to break out of the loop. Whether it is a for statement or a while statement, as long as the break statement is used in the loop, the program will immediately jump out of the current loop after executing the break statement and continue to execute.
The break statement is issued as follows:
break;
For example, in the following function we use the break statement, which breaks out of the loop when index=3. If this statement is not used, the while loop in the function cannot end until index=6. The procedure is as follows:
funcition testBreak(x){
var index=0;
while(index<6){
if(index==3)break;
index++
};
retrun index*x;
;
Continue statement
The functions of the continue statement and the functions of the break statement look somewhat similar, but in fact they are different. When a break statement is executed, it usually jumps out of the current loop, but the loop is executed until the continue statement does not jump out of the current loop, but does not execute the code blocks behind the continue statement in the loop, directly end the current round of the loop, and then immediately start the next round of the loop.
In the loop of a while statement, after encountering a continue statement, the program will directly judge the loop conditions and start the next loop. In the loop of the for statement, after encountering the continue statement, the program will directly execute the incremental expression, and then judge the loop conditions to start the next loop.
For example, we want to use a for loop to find the sum of even numbers between 1 and 10, and its WML Script statement is as follows:
var sum=0;
for (var j=1;j<=10;j++){
if(j%2!=0)
continue;
sun+j;
};
In this example, in the case of j%2!=0, that is, when j is an odd number, the program executes the continue statement. At this time, it does not jump out of the loop like a break statement. Instead, it does not execute the subsequent statement in the loop and directly executes the incremental expression to start the execution of the next loop. In this way, the number of j and the like can be noted as in the sum.
For example, if we want to use the while loop to find the sum of several numbers other than 3 between 0 and 4, we can use the continue statement for control. The procedure is as follows;
var index=0;
var count=0;
while (index<5){
index++;
if(index==3)
continue;
cont+=index;
};
This is true in the program, when index equals 3, "index==3" is true, so the continue statement is executed, and the value of index is no longer increased in the count at this time, but the next round of loop begins.
Previous page123456789Next pageRead the full text