Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • dislike -8 28

    Learn Computer Science With JavaScript: Part 3, Loops

    Introduction

    Suppose you have been given the task to write a program that displays the numbers 1–100. One way you could accomplish this is to write 100 console.log statements. But I’m sure you wouldn’t because you would have become fed up by the 9th or 10th line.  

    The only part that changes in each statement is the number, so there should be a way to write only one statement. And there is with loops. Loops let us perform a set of steps in a code block repeatedly.  

    Contents

    • While loops
    • Do-while loops
    • For loops
    • Arrays
    • For-in loops
    • For-of loops
    • Review
    • Resources

    While Loops

    While loops will execute a set of statements repeatedly while some condition is true. When the condition is false, the program will exit the loop. This kind of loop tests the condition before performing an iteration. An iteration is an execution of the loop’s body. The following example will not display anything because our condition is false.

    This is the general form of a while loop:

    One thing to be careful of when using while loops is creating loops that never end. This happens because the condition never becomes false. If it happens to you, your program will crash. Example:

    Task

    How many times will the body of this loop be executed:

    Do-While Loops

    A do-while loop will execute the body of statements first, and then check the condition. This kind of loop is useful when you know you want to run the code at least once. The following example will display “eat” once, even though the condition is false.

    This is the general form for a do while-loop:

    Task

    Write a do-while loop that will display the numbers 1–10.

    For Loops

    A for-loop will repeat execution of a code block for a specific number of times. The following example displays the numbers 1–10:

    This is the general form of a for-loop:

    Initial is an expression that sets the value of our variable. Condition is an expression that must be true for the statements to execute. And step is an expression that increments the value of our variable.

    One programming pattern is to use a for loop to update the value of a variable with itself and a new value. This example sums the numbers 1–10:

    The += is an assignment operator that adds a value back to a variable. This is a list of all the assignment operators:

    Operator
    Example
    Equivalent
    +=x += 2
     x = x + 2
    -=x -= 2
    x = x - 2
    *=x *= 2
    x = x * 2
    /=x /= 2
    x = x / 2
    %=x %= 2
    x = x % 2

    Task 

    Write a for loop that calculates the factorial of a number. The factor of a number n is the product of all the integers from 1 to n. For example, 4! (4 factorial) is 1 x 2 x 3 x 4 which equals 24.

    Arrays

    An array is an object that holds a list of items, called elements, which are accessed by their index. The index is the position of the element in the array. The first element is at the 0 index. The following are some common array operations.

    Create an empty array:

    Initialize an array with values:

    Get an element from an array:

    Update an element in an array:

    Loop over an array:

    A two-dimensional array is an array whose elements are arrays. Example:

    This is how you would loop over the array and display each element:

    Task 

    What element is displayed when i = 1 and j = 0 in the above for loop?

    For-In Loop

    This kind of loop lets us loop through the keys in an object. An object is a data structure that has keys mapped to values. Here are some common operations that can be performed on an object.

    Create an empty object:

    Initialize an object with values:

    Get a property from an object:

    Update a property in an object:

    Loop over the keys of an object:

    Task

    What does the above for loop display given obj = {foo: "Hello", bar: "World"}?

    For-Of Loop

    This kind of loop lets us loop over the values of iterable objects. Examples of iterable objects are arrays and strings.

    Loop over an array:

    Loop over a string:

    Task

    Using any of the loops, write a program that will display this staircase pattern:

    Review

    Loops let us reduce duplication in our code. While loops let us repeat an action until a condition is false. A do-while loop will execute at least once. For loops let us repeat an action until will we reach the end of a count. The for-in loop is designed so we can access the keys in an object. The for-of loop is designed so we can get the value of an iterable object. 

    Next, in part 4, we will learn about functions.

    Resources

    martijn broeders

    founder/ strategic creative at shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 434 210 0245

By - category

    By - date