Day 64: Conditional Statements

If statements

In JavaScript, if statements are used to execute a block of code only if a certain condition is true. Here’s the basic syntax for an if statement:

if (condition) {
  // Code to be executed if condition is true
}

Here, condition is any expression that can be evaluated as either true or false. If the condition is true, then the code inside the curly braces will be executed. If the condition is false, nothing will happen and the program will move on to the next line of code.

You can also add an optional else clause to your if statement, which allows you to execute a different block of code if the condition is false:

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

Here’s an example that uses an if statement to check whether a user’s age meets a certain requirement:

var age = 25;

if (age >= 18) {
  console.log("You are old enough to vote");
} else {
  console.log("Sorry, you are not old enough to vote yet");
}

In this example, we’re checking whether the value of the age variable is greater than or equal to 18. If it is, we print out a message saying “You are old enough to vote”. If it isn’t, we print out a different message saying “Sorry, you are not old enough to vote yet”.

You can also use logical operators (&&, ||, and !) with your conditions in order to test for more complex combinations of conditions. Here’s an example that uses both an if statement and logical operators:

var age = 25;
var hasLicense = true;

if (age >= 16 && hasLicense) {
  console.log("You are old enough and have a license");
} else {
  console.log("Sorry, you cannot drive yet");
}

In this example, we’re checking whether both the value of the age variable is greater than or equal to 16 AND whether the value of the hasLicense variable is true. If both conditions are met, we print out a message saying “You are old enough and have a license”. Otherwise, we print out another message saying “Sorry, you cannot drive yet”.

If-else statements

In JavaScript, if-else statements are used to execute one block of code if a condition is true and another block of code if the condition is false. Here’s the basic syntax for an if-else statement:

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

Here, condition is any expression that can be evaluated as either true or false. If the condition is true, then the code inside the first set of curly braces will be executed. If it’s false, the code inside the second set of curly braces will be executed.

You can also add multiple else-if clauses to your if-else statement in order to test for more than two conditions. Here’s an example:

var grade = 85;

if (grade >= 90) {
  console.log("A");
} else if (grade >= 80) {
  console.log("B");
} else if (grade >= 70) {
  console.log("C");
} else if (grade >= 60) {
  console.log("D");
} else {
  console.log("F");
}

In this example, we’re checking the value of a variable called grade, and printing out a different message depending on what that value is. If grade is greater than or equal to 90, we print out “A”. If it’s between 80 and 89, we print out “B”, and so on.

It’s important to note that only one block of code – at most – will ever be executed in an if-else statement. Once a condition has been met and its corresponding block of code has been executed, all other conditions will be skipped.

You can also use nested if-else statements in order to test for even more complex combinations of conditions. However, as with any kind of nesting, it’s important to keep your code organized and easy to read.

Switch statements

In JavaScript, switch statements are used to execute different blocks of code depending on the value of a variable or expression. Here’s the basic syntax for a switch statement:

switch (expression) {
  case value1:
    // Code to be executed if expression matches value1
    break;
  case value2:
    // Code to be executed if expression matches value2
    break;
  ...
  default:
    // Code to be executed if expression doesn't match any other cases
}

Here, expression is any variable or expression that you want to test against. The case keywords define different possible values that expression could have, and the corresponding blocks of code that should be executed in each case.

If none of the values match, then the default block of code will be executed instead. It’s important to note that you don’t need a break statement after the default block – it will automatically be executed if none of the other cases match.

Here’s an example that uses a switch statement to print out a different message depending on what day it is:

var day = "Monday";

switch (day) {
  case "Monday":
    console.log("Today is Monday");
    break;
  case "Tuesday":
    console.log("Today is Tuesday");
    break;
  case "Wednesday":
    console.log("Today is Wednesday");
    break;
  case "Thursday":
    console.log("Today is Thursday");
    break;
  case "Friday":
    console.log("Today is Friday");
    break;
  default:
  	console.log("It's not a weekday!");
}

In this example, we’re testing the value of a variable called day, and printing out a different message depending on what its value is. If it’s equal to "Monday", we print out “Today is Monday”, and so on.

One advantage of using a switch statement over multiple nested if-else statements is that it can often make your code cleaner and easier to read. However, it’s important to remember that both constructs have their own strengths and weaknesses, and there isn’t always one right way to write your code.

Exercise: Conditional statements

Write a program that determines the maximum of two numbers

Write a program that determines the grade for a given percentage