Lesson 5 of 16
In Progress

Control Structures (if/else, for and while loops)

In JavaScript, control structures are blocks of code that allow you to control the flow of your program. They enable you to make decisions based on certain conditions and to repeat a block of code multiple times.

There are several control structures in JavaScript, including if/else statements, for loops, and while loops. In this article, we’ll go over the basics of each of these control structures and how to use them in your code.

If/Else Statements

An if/else statement allows you to execute a block of code if a certain condition is true, or another block of code if the condition is false.

Here’s the syntax for an if/else statement:

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

Here’s an example of how to use an if/else statement:

var age = 25;
if (age > 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

In this example, the if block will be executed because the value of age is greater than 18.

You can also use multiple else if blocks to check for additional conditions:

var age = 25;
if (age < 18)
console.log("You are a minor.");
} else if (age < 21) {
  console.log("You are a young adult.");
} else {
  console.log("You are an adult.");
}

In this example, the else if block will be executed because the value of age is less than 21 but greater than or equal to 18.

For Loops

A for loop is used to execute a block of code multiple times. It consists of three parts: an initializer, a condition, and an incrementer.

Here’s the syntax for a for loop:

for (initializer; condition; incrementer) {
  // code to execute
}

Here’s an example of how to use a for loop:

for (var i = 0; i < 5; i++) {
  console.log("Hello, world!");
}

In this example, the for loop will execute the console.log statement five times, with the value of i starting at 0 and incrementing by 1 each time.

While Loops

A while loop is similar to a for loop, but it only has a condition. It will continue to execute a block of code as long as the condition is true.

Here’s the syntax for a while loop:

while (condition) {
  // code to execute
}

Here’s an example of how to use a while loop:

var i = 0;
while (i < 5) {
  console.log("Hello, world!");
  i++;
}

In this example, the while loop will execute the console.log statement five times, with the value of i starting at 0 and incrementing by 1 each time.

Conclusion

Control structures are an essential part of JavaScript programming. By understanding how to use if/else statements, for loops, and while loops, you’ll have the skills you need to build complex and interactive web applications with JavaScript.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Use an if/else statement to check whether a number is even or odd.

var num = 10;
if (num % 2 == 0) {
  console.log("The number is even.");
} else {
  console.log("The number is odd.");
}

Use a for loop to print the numbers 1 to 10 to the console.

for (var i = 1; i <= 10; i++) {
  console.log(i);
}

Use a while loop to print the numbers 1 to 10 to the console.

var i = 1;
while (i <= 10) {
  console.log(i);
  i++;
}

Use a for loop to print the numbers 10 to 1 to the console.

for (var i = 10; i > 0; i--) {
  console.log(i);
}

Use a while loop to print the numbers 10 to 1 to the console.

var i = 10;
while (i > 0) {
  console.log(i);
  i--;
}