Loop Statements
Loop Statements
Rating:
Loops are the statements that are used to run a set of statements several times until a condition is true. These statements can also be used to extract the elements of an array. A basic loop statement has three parts: the condition, the list of statements to be repeated, and a counter update that controls how many times the loop executes.
There are four types of loops, for loop, for..in loop, while loop and do..while loop.
The for loop is created by using the for keyword. The syntax of using the for loop is as follows:
{
//statements
}
In the above syntax, the initialization refers to the value that starts the loop. The condition is the conditional statement that is used to control the loop. If the condition is true, the loop continues. If the condition is false, the loop is terminated and the control passes to the next statement that follows the loop block. The increment or decrement is used to run the loop in forward or backward direction.
The for loop performs the following four steps until the condition returns false:
- When the loop starts, the initialization expression is executed.
- The condition is evaluated. If condition returns true, go to step 3. Otherwise, execute the next statement following the for loop.
- Run the statement inside the loop block.
- The increment expression executes and then go back to step 2.
The following code will demonstrate the use of the for loop:
for(a=1; a<=10; a++)
{
trace(a);
}
The above code will display a series from 1 to 10 on the output window.
The syntax of using the for in loop is as follows:
{
//statements
}
The following code will demonstrate the use of for in the loop:
obj.name="Jason";
var result="";
var I
for(i in obj)
{
result+=obj.name+"."+i+"="+obj[i]+ "<br>";
}
trace(result);
The above code will display "Jason.name=Jason<br>" on the output window.
In this loop the variable needs to be initialized before the start of the loop, and the variable increments each time the statement is executed. When the value of the variable reaches a specified value, the condition returns false and the loop ends.
The syntax of the while loop is as follows:
{
//statements
}
The loop performs the following steps:
- Evaluate the condition.
- If condition returns true, go to step 3. Otherwise, execute the statements following the end of the while loop.
- Execute the loop statement.
- Go back to step 1.
The following example will demonstrate the use of the while loop:
while(num<=10)
{
trace(num);
}
The above statement will display a series from 1 to 10 on the output window.
In the do while loop, the variable needs to be initialized and increments each time the statements are executed. When the value of variables reaches a specified value, the condition returns false and the loop ends.
The syntax of the do-while loop is as follows:
{
//statements
} while(condition)
The loop performs the following four steps:
- Execute the statements inside the loop.
- Evaluate the condition.
- If the condition returns true, go to step 4. Otherwise, execute the next line following the end of the do while loop.
- Go back to step 1.
The following example will demonstrate the use of the do-while loop:
do
{
trace(num);
}while(num<=10)
The above statement will display a series from 1 to 10 on the output window.
The syntax of using the break statement is as follows:
{
//statements
if(condition)
{
break;
}
}
The following code will demonstrate the use of the break statement:
for(a=1; a<=10; a++)
{
trace(a);
if(a==5)
{
break;
}
}
The above code will display a series from 1 to 5 on the output window.
The syntax of using the continue statement is as follows:
{
//statements
if(condition)
{
continue
}
}
The following example will demonstrate the use of the continue statement:
for(a=0;a<10;a++)
{
if(a==5)
{
continue;
}
trace(a);
}
In the above code, the series from 1 to 4 will be displayed on the output window, the loop will break at 5, and again the series from 6 to 10 will be displayed.
The syntax of using the with statement is as follows:
{
//statements
}
The following example will demonstrate the use of the with statement:
{
_x= 135;
_y=452;
}
Rating:
Other articles
- Tip for removing all hints from an object at the same time
- What is the Brush tool?
- Tip for opening the Layer Properties dialog box
- What is the leading property?
- What is SWF?
