JavaScript - Sequence, Selection & Iteration

Around every section of Javascript code, you must include the <script> tags.

Sequences
A sequence is a set of instructions executed one after the other. A source code is a collection of instructions whose sequence is the order in which it has been written. The processor will then read it in order from beginning to end, executing the code as it goes.

Example
<script>
    var a = 1;
    var b = 2;
    var c = 3;
    a = b;
    c = a;
<script>

Firstly the processor assigns the values 1-3 to the variables a,b and c. Then it reassigns the value a to be the same as b and reassigns value c to be the same as a. As the reassignment of c occurred after the reassignment of c, c takes the new value of a. So in this case a=b=c=2.

Selection
A selection is a conditional statement. We can make a machine do something and stop doing something, but we can also tell the machine when to stop doing that process when a certain condition (or conditions) is met.
This can be done with an if statement. This states a condition that must be met and then tells the machine what you want it to do.

Example
<script>
    var a = 1;
    var b = 2;
    if (a         {
        var text = “I know a < b, it’s a bit obvious”;
        document.getElementById(“demo”).innerHTML =text;
        }
</script>

This code will print var text whilst the codition is still being met and as the variable are staying the same not being reassigned the condition is always true. We can also include an ‘else’ statement that will tell the machine what it should do if the condition is false.

When it comes to doing operations and comparisons with variables, the symbols that are used are slightly different to those you would use in maths. “=” no longer means equal to, but is used as a way to assign a variable to a value or character. To compare equality between variables we use “==” and to show that two variables are not equal we use “!=”. You are not only comparing the values but the type as well. A string cannot be equal to an integer even if it looks the same.

Iteration
This allows us to repeat a certain statement for a certain number of times whilst the condition is true. We can use a while loop for this.

Example
<script>
    var a = 0;
    var b = 10;
    result = “a < b”;
    text = “ “;
    while(a < b)
        {
        text += result + “<br>”;
        a++;
        }
    document.getElementById(“demo”).innerHTML = text;
</script>

This section of code will print the statement, “a < b” ten times as that is as many times that the loop can run with the condition being true. This is because time the loop runs, the ‘a’ variable increases by 1 until it is no longer smaller than ‘b’.

This effect can also be achieved by using a for loop which intialises and increments the value of a all in one go. So the equivalent using the for loop would look like this:

for(a = 0; a < b; a++)
    {

    }

Loops are very useful for doing very repetitive pieces of code that you don’t want to write out over and over again. They can also be used to solve mathematical problems.
E.g. If we have an equation 3 + y = 74 and we want to find the value of y that will solve the equation, we can use a while loop that will plug numbers into equation until it finds the right one.

Example
<script>
    var y = 0;
    for(y = 0; y < 74; y++)
        {
        if(3 + y == 74)
            {
            document.getElementById(“demo”).innerHTML = y;
            }
        }
</script>

Exercise:
1) Write a loop that writes out your name the same number of times as your age. Define age at the beginning and assign it to the variable x.

2) Write and use a while loop to find an integer x such that (x + 3)*5 = 50.



← Go back to Index Page Click Here!

Back to the Intro Page