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 attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
While loops

Last accessed pages

  1. Ajax-PHP Chat Script (49553)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (143470)
  3. Output or Force Download MP3 with PHP (5831)
  4. Area and Perimeter Calculator for 2D shapes (10182)
  5. Display data from PHP Array, or MySQL in HTML table (27013)

Popular pages this month

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