# Control structures

# Selection

  • In JavaScript, you can use the following (classical) selection structures

# if

if (condition1) {
  //  block of code to be executed if condition1 is true
}
Copied!
1
2
3

# if - else

if (condition1) {
    //  block of code to be executed if condition1 is true
} else {
    //  block of code to be executed if condition1 is false
}
Copied!
1
2
3
4
5

# if - else if - else

if (condition1) {
    //  block of code to be executed if condition1 is true
}
else if (condition2) {
    //  block of code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
    //  block of code to be executed if condition1 and condition2 are false and condition3 is true
} else {
    //  block of code to be executed if none of the previous conditions are true
}
Copied!
1
2
3
4
5
6
7
8
9
10

# switch

switch (expression) {
    case x:
        // block of code to be executed if case x is true
        break;
    case y:
        // block of code to be executed if case y is true
        break;
    default:
        // block of code to be executed if none of the previous cases are true
}
Copied!
1
2
3
4
5
6
7
8
9
10

WARNING

Don't forget the break at the end of each case!

# Iteration

  • Also the following (classical) iteration structures are available in JavaScript

# for

const end = 10;
for (let i = 0; i <= end; i++) {
  // block of code to be executed
}
Copied!
1
2
3
4

# while

let i = 0;
const end = 10;
while (i <= end) {
	 // block of code to be executed
	i++;
}
Copied!
1
2
3
4
5
6

# do while

let i = 0;
const end = 10;
do {
	 // block of code to be executed
	i++;
} while (i <= end) 
Copied!
1
2
3
4
5
6

REMARK

Ado wile loop always iterates at least one time, even if the condition is false!

# Iterating arrays

  • There are several ways to loop over an array, but the most interesting (and the only one we use in this course) is the forEach (opens new window) iteration
  • The callback function can have three parameters:
    • element: the current element being processed in the array
    • index (optional): the index of the current element
    • array (optional): the full array on which forEach is performed
const array = ['element1', 'element2', 'element3', ...];
array.forEach(function(element, index, array) {
    // block of code to be executed on the current element
})
Copied!
1
2
3
4

# Examples

# Selection with 'if - else if'

  • Open es6/controls/else_if.html and es6/controls/else_if.js
  • Leave the input field empty or enter a number between 0 and 20 and check if you pass/fail the JavaScript course


 

 

<div class="border-gray">
    <label for="score">Score:</label>
    <input type="number" min="0" max="20" id="score" name="score" />
</div>
<div id="result"></div>
Copied!
1
2
3
4
5

# Selection with 'switch'

  • Open es6/controls/switch.html and es6/controls/switch.js
  • In this example, we replaced the numeric field with a range slider
    • This removes the need for an additional test to see if the field is empty


 
 
 
 

 

<div class="border-gray">
    <label for="score">Score:</label>
    <input type="range" id="score" name="score" min="0" max="20" value="14"
        oninput="output.value =  score.value"
    />
    <output id="output">14</output>/20
</div>
<div id="result"></div>
Copied!
1
2
3
4
5
6
7
8

# Compare iteration methods

  • Open es6/controls/drawBlocks.html and es6/controls/drawBlocks.js
<html lang="en">
    <head>
        ...
        <style>
            section span {
                display: inline-block;
                width: 3rem;
                line-height: 3rem;
                text-align: center;
                border: 1px solid orangered;
                background-color: rgba(255, 69, 0, 0.1);
                margin: 0 0.5rem 0.5rem 0;
            }
        </style>
    </head>
    <body>
        ...
        <div class="border-gray">
            <label for="block">Draw blocks:</label>
            <input type="range" id="block" name="block" min="0" max="20" value="6" oninput="output.value =  block.value"/>
            <output id="output">6</output>
        </div>
        <div class="row">
            <section class="col-sm-4">
                <h4>for</h4>
                <div></div>
            </section>
            <section class="col-sm-4">
                <h4>while</h4>
                <div></div>
            </section>
            <section class="col-sm-4">
                <h4>do while</h4>
                <div></div>
            </section>
        </div>
        ...
    </body>
</html>
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# Iterating arrays

Open es6/controls/colorBlocks.html and es6/controls/colorBlocks.js

<html lang="en">
    <head>
        ...
        <style>
            section div {
                line-height: 3rem;
                text-align: center;
                background-color: rgba(115, 115, 115, 0.1);
                margin: 1rem;
            }
        </style>
    </head>
    <body>
        ...
        <section id="blocks"></section>
        ...
    </body>
</html>
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Exercises

  • Open exercises/index.html and make the exercises under the tab ES6 > Controls
Last Updated: 4/20/2022, 1:45:58 PM