Javascript Course


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

The while() loop

The while() loops executes a code as long as a condition is True. If the condition starts out as false, the statements won’t execute at all.

- Syntax:
while(condition){
 //code to be executed
}
Example:
<script>
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.

do ... while() loops

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

Syntax:
do {
 //code to be executed
}
while(condition)
Here is a simple example:
<script>
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 between braces is still executed once.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
While loops

Last accessed pages

  1. Create and Use in PHP a Simple Template Page (3306)
  2. Common PHP Errors and Solutions (9771)
  3. PHP getElementById and getElementsByTagName (49471)
  4. Mysql SELECT JOIN tables on two different Databases (4498)
  5. jQuery UI draggable - Drag elements (11448)

Popular pages this month

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