Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Tutorials listed in this section have not been reviewed nor are affliated by Code Forum. It is the sole responsbility of the authour to properly maintain and ensure information is accurate.

Using loops

Basic understanding of loops and how they work​

Not all loops may be covered in this tutorial, as I learn them I'll update this tutorial.​

This tutorial will cover loops and how they work. Although this tutorial will be using JavaScript as the code example, the same principal should work with other languages.

Loops can be used to perform a variety of tasks such as looping through objects, performing iterations and performing while a particular condition is true.

So let's get started.

for loops


For loops are generally used to loop through code for an x amount of times. A loop uses 3 parameters, the initialization, the condition and the control separated by a semi-colon.

JavaScript:
for(var i = 0; i < 5; i++) {
    // do something
}

As shown above, we start with for followed by () and {} brackets. Within the first set of brackets (), we add our initialization variable, in most cases you might just want to declare a new variable directly inside or you may want to pass through a variable. But for this example we will declare a variable within using var i = 0;.

Next up is the condition. The condition is used to indicate that if it's true then continue to run through the loop, but if it becomes false then exit the loop. Note: be sure not to create an infinite loop! In the above example, i &lt; 5; is used, this will loop through the for loop until i is no longer less than 5 - meaning it will loop through 4 times before stopping.

Finally, the control is used to modify the initialization variable to essentially control the loop. In the above code example, we used i++; which will add one value to i each time the loop ends. So if we start at var i = 0;, each time it reaches the end of the loop the i++ will add one additional count, making i = 1;. It will continue this process until i is no longer less than 5.

An example of this:
Code:
var i = 0; i < 5; i++
Each line is at the end of each loop:
i = 1;
i = 2;
i = 3;
i = 4; // loops ends here as it's less than 5.

Then within the curly brackets {}, we add our code that we want to execute. Each time the loop is entered this code will be executed.

while loops


While loops, executes the code while the condition is still true. As long as that condition is true, the code within the while loop will be executed.

JavaScript:
while (light == true){
    console.log("light is on!");
}

In the above example, we start with while followed by () and {} brackets. Inside the () brackets, we add our condition - in the code provided above we have light == true so as long as light equals to true the code within {} will execute. But when it becomes false it will no longer execute the code within {}.

A few examples
This will execute
JavaScript:
var light == true;

while (light == true){
    console.log("light is on!");
}

This will NOT execute
JavaScript:
var light == false;

while (light == true){
    console.log("light is on!");
}

do while loops


Do while loops will execute the code first before checking the while condition. If the condition is true, it will run through the loop again, but if the condition is false the loop will be exited.

JavaScript:
do {
    // do something
} while (isSunOut == true);

In the above code example, we start off with do followed by {} curly brackets, then outside those closing curly brackets we have while with the trailing () brackets.

Within the () brackets after while, we have a condition similar to the other loops. In the code example, we have isSunOut == true. So as long as isSunOut == true, the code within the curly brackets {} will continue to execute after the initial execution of the code until the condition is no longer true.

Remember the do { } will execute even before checking the condition!

Here are a few examples
The first code example shows when the condition is false, along with what is displayed.
JavaScript:
var isSunOut == true;

do {
    console.log("Hello Sunshine!")
} while(isSunOut == false);

C:
Console:
Hello Sunshine!

The second code example below shows when the condition is true, console example is provided as well.
JavaScript:
var isSunOut == true;

do {
    console.log("Hello Sunshine!")
} while(isSunOut == true);

C:
Console:
Hello Sunshine!
Hello Sunshine!
Hello Sunshine!
Hello Sunshine!
Hello Sunshine!
Hello Sunshine!
so on
 
Back
Top Bottom