Can you give an example of an 'else if' statement?

0139

Certainly! Here's an example of an else if statement in JavaScript that evaluates a score and classifies it into different grade categories:

let score = 85;

if (score >= 90) {
    console.log("Excellent! You got an A grade.");
} else if (score >= 80) {
    console.log("Great job! You got a B grade.");
} else if (score >= 70) {
    console.log("Good work! You got a C grade.");
} else if (score >= 60) {
    console.log("You passed. You got a D grade.");
} else {
    console.log("Sorry, you failed the exam.");
}

In this example, the program checks the value of score against multiple conditions. The first condition that evaluates to true will execute its corresponding code block, and the rest will be skipped. If you run this code with a score of 85, the output will be:

Great job! You got a B grade.

0 Comments

no data
Be the first to share your comment!