Flow Control Structures in Javascript

Página 5

The flow control structures in Javascript are fundamental for the development of web sites. They allow the programmer to control the execution of the code, according to the established conditions. There are three flow control structures in Javascript: if/else, switch, and loops.

If/Else

The if/else structure is used to make decisions based on a condition. It allows code to perform one action if the condition is true and another action if the condition is false. The syntax is as follows:

if (condition) {
  // code to be executed if condition is true
} else {
  // code to be executed if condition is false
}

Switch

The switch is used when there are several conditions to be tested. It allows code to perform different actions based on the value of a variable. The syntax is as follows:

switch(variable) {
  case value1:
    // code to be executed if the variable equals value1
    break;
  case value2:
    // code to be executed if the variable equals value2
    break;
  default:
    // code to be executed if the variable is not equal to any of the previous values
    break;
}

Loops

Loops are used to execute the same block of code multiple times. There are two types of loops in Javascript: while and for.

While

The while is used when you don't know how many times the block of code will be executed. It executes the block of code as long as the condition is true. The syntax is as follows:

while (condition) {
  // code to be executed while the condition is true
}

For

The for is used when you know how many times the block of code will be executed. It executes the block of code a specified number of times. The syntax is as follows:

for (initialization; condition; increment) {
  // code to be executed while the condition is true
}

In summary, flow control structures in Javascript are essential for web site development. They allow the programmer to control the execution of the code, according to the established conditions. It is important to know each one of them well in order to use them efficiently.

Now answer the exercise about the content:

_Which of the following flow control structures is used when there are several conditions to be tested and allows the code to perform different actions according to the value of a variable?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

6Functions in Javascript

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text