Skip to content

Latest commit

 

History

History
253 lines (218 loc) · 5.22 KB

09. if_else Statement.md

File metadata and controls

253 lines (218 loc) · 5.22 KB

if/else Statement

In JavaScript, the if/else statement is used to make decisions in your code based on a condition. It allows you to execute one block of code if a certain condition is true and another block of code if the condition is false. Here's the basic syntax of the if/else statement:

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

Here's a breakdown of how it works:

  1. The if keyword is followed by an expression enclosed in parentheses. This expression is the condition you want to evaluate. If the condition is true, the code inside the first block (enclosed by curly braces) will be executed. If the condition is false, the code inside the else block (enclosed by curly braces) will be executed.

  2. You can omit the else block if you only want to execute code when the condition is true. In that case, if the condition is false, nothing will be executed.

Here's an example:

let age = 18;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are not an adult.");
}

In this example, if the age is greater than or equal to 18, it will print "You are an adult." Otherwise, it will print "You are not an adult."

You can also have multiple if conditions using else if:

let score = 75;

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

This code will evaluate the score variable and print the corresponding grade based on the value of score.

if statement

Syntax

if (test_condition) {
    // if statement body
}

const score = parseInt(prompt('Enter your marks:'));

if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}

Output

Enter your marks: 80
You have passed your exams.
Congratulations.

Multiple if statement

const score = parseInt(prompt('Enter your marks:'));

if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}

if (score < 50) {
    console.log('Sorry, you failed your exam.');
}

Output

Enter your marks: 40
Sorry, you failed your exam.

if...else Statement

Syntax

if (condition) {
  // code inside if block
}
else {
  // code inside else block
}

const score = 40;

if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}

else {
    console.log('Sorry, you failed your exam.');
}

Output

Sorry, you failed your exam.

else if Statement

Syntax

if (test_condition1) {
    // statements1
}
else if (test_condition2){
    // statements2
}
else {
    // statements3
}

const score = 40;

if (score > 100) {
    console.log('Score is invalid');
}

else if ( score < 0) {
  console.log('Score is invalid');
}

else if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}
else {
    console.log('Sorry, you failed your exam.');
}

Output

Sorry, you failed your exam.

const score = -45;

if (score > 100) {
    console.log('Score is invalid');
}

else if ( score < 0) {
  console.log('Score is invalid');
}

else if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}
else {
    console.log('Sorry, you failed your exam.');
}

Output

Score is invalid

Logical OR operator

const score = -45;

if (score > 100 || score < 0) {
    console.log('Score is invalid');
}

else if (score >= 50) {
    console.log('You have passed your exams.');
    console.log('Congratulations.');
}
else {
    console.log('Sorry, you failed your exam.');
}

Output

Score is Invalid.

Assignment and Task

Can you create a program to check whether a number is positive or negative or 0? To create this program, create a variable named number and assign a value to it based on the user input. Then using a if statement, check if the number variable is positive or negative or 0. If the number is positive, print "The number is positive." If the number is negative, print "The number is negative." (and) If the number is 0, print "The number is 0."

Solution:

const number = parseInt(prompt('Enter a number:'));

if (number > 0) {
    console.log("The number is positive.");
}

else if ( number < 0) {
  console.log("The number is negative.");
}

else {
    console.log('The number is 0.');
}

Output

Enter a number: 40
The number is positive

p4n Quiz

Q. What is the output of the following code?

const time = 15;

if (time < 10)
  console.log("Good Morning");
else if (time < 19) 
  console.log("Good Evening");
else if (time < 22)
  console.log("Good Night");
else
  console.log("Invalid Time");
  1. Good Morning
  2. Good Evening
  3. Good Night
  4. Invalid Time

Answer: 2