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
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient#id {
background:url("path_to_image.png");
background-size:contain;
background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()$nr = ceil(3.5);
echo $nr; // 4