Loop Control Structures (while, do..while, for..in)
Site: | Philippine Science High School - MC - Knowledge Hub |
Course: | SY25.CS3.Web Development |
Book: | Loop Control Structures (while, do..while, for..in) |
Printed by: | , Guest user |
Date: | Wednesday, 6 August 2025, 7:31 PM |
1. Introduction
After completing this module, you are expected to:
- Create corresponding loop statements to represent the iterative process.
- Use while, do while, and for..in statements to repeatedly execute codes.
While writing a program, you will encounter a situation where you would like to perform an action several times and in this instance this, you would get to use a loop statement to iterate through several lines of code. A loop statement is a control flow that executes a statement or a series of statements repeatedly while a specific condition is true. There are three types of loop statements; while statement, do/while statement, and for statement. JavaScript supports all the required loops to ease down the pressure of programming.
We are just going to look at the while, do while, and the for..in statements in this learning module.
2. while Statement
The while statement is one of the simplest types of loop statements, which repeats a statement or sequence of statements as long as they are evaluated by a given conditional expression to a truthy value.
Syntax:
In the while statement, the conditional expression is enclosed within a parenthesis following the keyword while. In general, a conditional expression contains a variable whose value in the statement code is modified by one or more statements. Each time the statements finish executing, the expression is reevaluated.
If a true value is determined by the expression, the statements are executed again; if a false value is evaluated by the expression, then the loop ends. Each repetition of a looping statement is known as iteration.
You must have a code that monitors the progress of the loop and adjusts the value provided by the conditional statement to ensure that the iteration ends after the desired tasks have been completed. You can monitor the progress with a counter for a while statement, each iteration of a loop statement, a counter which is a variable, is incremented (increased) or decremented (decreased) until the desired value is reached that would make the while condition to be false and end the loop.
The following code shows a simple script containing a while statement. The script declares the count variable and assigns it an initial value of 1. The count variable is then used in the condition (count <= 5) for the while statement. The while statement loops as long as the count variable is less than or equal to 5. The document.write () statement prints the value of the count variable within the body of the while statement, then the count variable increments by a value of 1. The while statement loops until a value of 6 is reached by the count variable.
while (count <= 5) {
count++;
document.write("You have printed 5 numbers.");
The code prints the numbers 1 through 5, with one iteration of the loop representing each number. When the count hits 6, the "You have printed 5 numbers" message prints, thus showing that the loop is over.
Output:

In a while loop, you can also control the repetitions by decreasing (decrementing the value of) the count variable.
while (count > 0) {
count--;
document.write("We have taken off. ");
In this case, the count variable's initial value is 10 and the decrement operator (--) is used to reduce the count by one. If the count variable is greater than 0, the value of the count variable is shown by the expression in the while loop. The while loop ends when the count value is equal to 0, and the statement immediately after the while loop is printed on the browser.

To control the repetitions of a while loop, there are
several ways to change the value of a count variable and to use it. The example
below uses the assignment operator *= to multiply by 2 the value of the count
variable. The while statement ends when the count variable reaches a value of
128, which is greater than 100, and the value 128 is not printed.
while (count <= 100) {
count *= 2;
As mentioned before, you must have a code inside the command block that changes the value used by the conditional expression, to ensure that a while statement ends eventually. Your program will be stuck in an infinite loop if you do not include code that changes this value. A loop statement never ends in an infinite loop because its conditional expression is never falsified.
window.alert("The number is " + count + ".");
Although a conditional expression that checks the value of a count variable is included in the while statement in the previous example, there is no code within the command block that changes the value of the count variable. Through each iteration of the loop, the count variable will continue to have the same value. That means, no matter how many times the user clicks the OK button, a warning dialog box containing the text string 'Number is 1.' will appear over and over again.

3. do while Statement
The do while statement is another
looping JavaScript statement, similar to the while statement. The do while statement
executes a statement or statements once, then repeats the execution as long as
it evaluates the true value of a given conditional expression.
The syntax is as follows for the do while statement:
Far from the simpler while statement, the statements in a do while statement are always executed at least once, before the conditional expression is tested the first time.
The following do while statement runs once before the count variable is evaluated by the conditional expression. Thus, a single line reads "The count equals 2." prints. The count variable is equal to 2 when the conditional count < 2 is evaluated. This allows a false value to return to the conditional expression, and the do while statement ends.
do {
count++;

In the next example, the code
prompts the user for a multiplier, and prints the multiples of 1 to 50 using a
do while loop.
var multiplier = prompt("Enter a multiplier: ");
</script>
4. for..in Loop
The for..in loop is a more concise
way of iterating over an object's properties. The for...in loop will run for
all of the object's elements, and its syntax will be as follows:
Syntax:
Rank = {"Rank1": "Legendary", "Rank2": "Master", "Rank3": "Pro", "Rank4": "Elite", "Rank5": "Veteran", "Rank6": "Rookie"};
// Iterate over the properties.
var cod = "";
for (var codRank in Rank) {
cod += codRank + ": " + Rank[codRank];
cod += "<br />";
}
document.write(cod);
Output:
Example 2: shows how to initialize the array with expanding properties to the array and iterate over the properties and elements using the for..in loop.
var arr = new Array("Legendary", "Master", "Pro");
// Add a few expand properties to the array.
arr["Rank3"] = "Elite";
arr["Rank4"] = "Veteran";
// Iterate over the properties and elements.
var cod = "";
for (var codRank in arr) {
cod += "<br />";
document.write (cod);
Output:

5. Summary and References
Summary
In summary,
- A loop statement is a statement of control flow that executes a statement or a series of statements repeatedly while a specific condition is true.\
- The while statement is used to repeat a statement or series of statements as long as true is evaluated by a given conditional expression.
- An iteration is named for any repetition of a looping statement.
- An infinite loop is a case where the declaration of a loop never ends because the conditional expression is never false.
- The do/while statement executes a statement or statements once, and then repeats the execution as long as true is determined by a specified conditional expression.
- The for..in loop is a more concise way of iterating over an object's properties.
References
JavaScript Loops: (n.d.). Retrieve from https://www.tutorialrepublic.com/javascript-tutorial/javascript-loops.php
JavaScript Loops Explained: For Loop, While Loop, Do…while Loop, and More: (n.d.). Retrieve from https://www.freecodecamp.org/news/javascript-loops-explained-for-loop-for/