Actionscript Course

While loops

The while loops are used to repeat a block of code as long as a specified condition is true.
The while loops have the following syntax:

while (condition) {
  // codeBlock
}
- If the condition is true, the code within curly brackets is executed, then the script checks again the condition, and if it is still true, "codeBlock" is executed again. This process continues until condition becomes false, or you use a "break" statement to end the loop.

Example:
var total:int = 1;

// define a variable (counter) to control the executions number
var i:int=0;

// create a while loop
while(i<4)
{
  total *= 2;
  trace(total);
  i++;        // adds 1 to counter (i)
}

// In Output displays:  2 4 8 16
ActionScript first evaluates the condition (i<4), becouse "i=0", this condition returns true; so, ActionScript executes the loop body.
The loop body sets "total" to its own value multiplied by two, then "trace(total)" output its value, and "i++" adds one to "i" (the counter).
Then, ActionScript evaluates again the condition, and if it is true (now "i" is 1) executes the loop body. This process continues until the value of "i" variable is 4, which is not less than 4, so the condition is false, and the loop ends.
We got the following output:   2 4 8 16

Do While

The do while loop is a variant of the "while()" loop. The difference is that "do while" loop first executes the block of code, and then evaluates the condition.
The syntax is:
do {
  // codeBlock
} while (condition);
Example:
var total:int = 1;

// define a variable (counter) to control the executions number
var i:int=1;

// create a do..while loop
do
{
  total *= 2;
  trace('total is '+ total);
  i++;        // adds 1 to counter (i)
} while(i<0);

// In Output displays:  total is 2
This code displays in Output panel:   total is 2
As you can notice, although the condition is false (i<0 , but i=1), the code within curly brackets is still executed once.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
While and Do While

Last accessed pages

  1. JavaScript strip_tags and stripslashes (8825)
  2. Date and Time in ActionScript 3 (10086)
  3. Bind Tool and Control Points (4430)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142539)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (542)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)