5.4.3. else if Statements¶

/*If-else statements allow us to construct two alternative paths. 
A single condition determines which path will be followed. We can 
build more complex conditionals using an else if clause. These allow 
us to add additional conditions and code blocks, which facilitate more 
complex branching.*/

let x = 10;
let y = 20;

if (x > y) {
   console.log("x is greater than y");
} else if (x < y) {
   console.log("x is less than y");
} else {
   console.log("x and y are equal");
}
//x is less than y

0
0
Hannes101 100 points

                                    /*Regardless of the complexity of a conditional, no more than one of 
the code blocks will be executed.*/

let x = 10;
let y = 20;

if (x &gt; y) {
   console.log(&quot;x is greater than y&quot;);
} else if (x &lt; y) {
   console.log(&quot;x is less than y&quot;);
} else if (x % 5 === 0) {
   console.log(&quot;x is divisible by 5&quot;);
} else if (x % 2 === 0) {
   console.log(&quot;x is even&quot;);
}
//x is less than y

/*Even though both of the conditions x % 5 === 0 and x % 2 === 0 
evaluate to true, neither of the associated code blocks is executed. 
When a condition is satisfied, the rest of the conditional is 
skipped.*/

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source