Conteúdo do curso
JavaScript Course for Beginners: Fundamentals and Practice
Sobre a Aula

Conditional Structures: if, else if, else

Welcome to Module 1! In this lesson, you’ll unravel the power of conditional structures in JavaScript, essential tools for making decisions and controlling the flow of your code.

Get ready to explore the if, else if, and else statements, learning how to use them to create different scenarios in your scripts.

if Structure

The if structure allows you to execute a code block only if a certain condition is true. It’s like checking if it’s raining before leaving home with an umbrella.

Syntax:

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

Example:

if (age >= 18) {
  console.log("You can make the purchase.");
}

else if Structure

The else if structure allows you to check additional conditions when the previous one is not true. It’s like checking if it’s cloudy; otherwise, you continue checking other conditions.

Syntax:

if (condition1) {
  // code block to be executed if condition1 is true
} else if (condition2) {
  // code block to be executed if condition2 is true
}

Example:

if (age >= 18) {
  console.log("You can make the purchase.");
} else if (age >= 16) {
  console.log("You can make the purchase with parental authorization.");
} else {
  console.log("You cannot make the purchase.");
}

else Structure

The else structure is the last resort. If all the previous conditions are false, the code block inside else will be executed. It’s like not finding any favorable conditions and taking a default action.

Syntax:

if (condition1) {
  // code block to be executed if condition1 is true
} else if (condition2) {
  // code block to be executed if condition2 is true
} else {
  // code block to be executed if none of the above conditions is true
}

Example:

if (age >= 18) {
  console.log("You can make the purchase.");
} else {
  console.log("You cannot make the purchase.");
}

Now, an example to illustrate the use of if, else if, and else conditional structures:

Suppose we have a function called checkGrade that takes a student’s grade as a parameter and displays a message according to the range in which the grade falls.

If the grade is greater than or equal to 9, the function will display the message “Excellent!”. If the grade is between 7 and 8.9, the function will display the message “Congratulations!”. Otherwise, the function will display the message “Study more!”.

function checkGrade(grade) {
  if (grade >= 9) {
    console.log("Excellent!");
  } else if (grade >= 7 && grade < 9) {
    console.log("Congratulations!");
  } else {
    console.log("Study more!");
  }
}

// Example of using the function
checkGrade(9.5); // Output: "Excellent!"
checkGrade(7.8); // Output: "Congratulations!"
checkGrade(6.2); // Output: "Study more!"

In this example, we use if to check if the grade is greater than or equal to 9. If this condition is true, the “Excellent!” message will be displayed.

Otherwise, the next condition is checked using else if. If the grade is between 7 and 8.9 (inclusive), the “Congratulations!” message will be displayed.

Finally, if none of the above conditions is met, the code block inside else will be executed, displaying the “Study more!” message.

Challenge:

  1. Create a JavaScript script that prompts the user for their age.
  2. Use conditional structures to check if the user is an adult, a minor, or in the age range of 16 to 17 years old.
  3. Display a personalized message on the console according to the user’s age.

Remember:

  • Conditional structures are powerful tools to control the flow of your code.
  • Practice creating different scenarios with if, else if, and else to master the use of these structures.
  • Use logical operators to build complex conditions and make more precise decisions in your scripts.

This way, we can use the if, else if, and else conditional structures to handle different scenarios and make decisions based on multiple conditions.

Now that you understand conditional structures, you can make decisions in your code based on different conditions. In the next lesson, we’ll explore loop structures, which are like repetitions to perform repetitive tasks.

Entrar na conversa
Rolar para cima