A while loop just looks at a short comparison and repeats until the comparison is no longer True.

1. The while loop

The while loops don't necessarily use a variable to count. Instead, they execute as long as a condition is True. In fact, if the condition starts out as false, the statements won’t execute at all.

The while statement includes the condition in parentheses, and it is followed by a block of statements within braces. Here is its syntax:
while (condition) {
  code to be executed
}
Here's the earlier example, from the "for" instruction, this version use the "while" loop.
<script type="text/javascript"><!--
var i = 0;
while (i<5) {
  document.write("<br /> i = "+i);
  i++;
}
--></script>
- First it's declared a variable "i" with the value of 0. The "while" statement checks the condition (here i<5), which it's True and permits the execution of block code inside the brackets. The "i++" increments the value of "i" and check again the condition. The loop will stop when the "i" reach 5.
- This script displays the following results:
i = 0
i = 1
i = 2
i = 3
i = 4

2. do ... while loops

The do...while loop is a variant of the "while" loop. First will be executed the block of code, and then it will repeat the loop as long as the specified condition is true.

The syntax is:
do {
  code to be executed
}
while (condition)
Here is a simple example:
<script type="text/javascript"><!--
var x = 8;
do {
  document.write("<br /> x = "+x);
  x++;
}
while (x<5)
--></script>
- This example display "x = 8".
As you can notice, although the condition is false (x<5), the code of braces is still executed once.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute can be used to add CSS styles directly in the HTML tag?
type style class
<div style="width: 80%; border: 3px solid #888888;">Content</div>
Which CSS transform method distorts the HTML element in a given angle (including its content)?
translate() scale() skew()
#some_id {
  transform: skew(20deg, 25deg);
  -ms-transform: skew(20deg, 25deg);   /* IE 9 */
  -webkit-transform: skew(20deg, 25deg);   /* Safari and Chrome */
}
Click on the function which converts a Date object to a string.
indexOf() toString() getDate()
var rightnow = new Date();
alert( rightnow.toString() );
Which function applies a callback function to the elements of the given array?
array_merge() array_search() array_map()
$arr = arra("abc", "<p>xyz</p>", "<em>PHP</em>");
// apply the strip_tags() function to delete HTML tags from each array item
$arr = array_map("strip_tags", $arr);
}
While loops

Last accessed pages

  1. Get Lower, Higher, and Closest Number (1574)
  2. Mahjong Long (687)
  3. jQuery ajax() method (13416)
  4. SHA512 Encrypt hash in JavaScript (7775)
  5. Mahjong Games (17768)

Popular pages this month

  1. Qwop (3030)
  2. Drag Racer V3 (1516)
  3. Bubbles3 (727)
  4. Flash Games - Free online Games (677)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (665)