Latest posts

10/recent/ticker-posts

Conditional Statements in Javascript | if statements | if-else statements | else-if statements | Nested if-else statements | switch statements

Conditional statements are an essential part of programming, and JavaScript is no exception. JavaScript provides a set of conditional statements that allow you to execute different blocks of code based on certain conditions. In this blog post, we'll explore the different types of conditional statements in JavaScript and how they can be used in your code.

Basically, Conditional statements are used to check whether the given condition is true or false.
When the condition is true, then a specific task or block of code will get executed or either nothing happens or the false conditions part will be get executed.

In Javascript Programming, there are 5 types of Conditional Blocks available,
  1. if statement
  2. if - else statement
  3. else-if statement
  4. Nested if-else statement
  5. Switch-case statement
These blocks help the developers to do specific tasks in programming according to the conditions.

1. if statement :

if statement is the basic conditional block of Javascript, which is used to execute a block of code when the given condition is true.
In that, the developer has to give a specific condition to do any specific task at the run time of program execution.

Syntax :
if(condition)
{
    //Code to be executed when the codition is true.
}

Example :
For example, we want to develop a code to check, whether the user is an adult or not.
let age = 20;
if(age >= 18)
{
    document.write("You are an Adult !");
}



2. if-else statements :

The if-else statement allows developers to execute one block of code if the condition is true and another block of code if the condition is false.
By using this, developers can run the program without stopping the execution of code.

Syntax :
if (condition)
{
  // code to be executed if the condition is true
}
else
{
  // code to be executed if the condition is false
}

Example :
let age = 15;
if (age >= 18)
{
  document.write("You are an adult");
}
else
{
  document.write("You are not yet an adult");
}


3. else-if statement :

The if-else if-else statement allows you to execute one block of code if the first condition is true, another block of code if the second condition is true, and so on.

Basically, in that, the developer can passes more than 2 conditions to execute the code. It will help us to get the result from a number of conditions.
It will show the result of the if statement that matches the condition.

Syntax :
if (condition1)
{
  // code to be executed if condition1 is true
}
else if (condition2)
{
  // code to be executed if condition2 is true
}
else {
  // code to be executed if all conditions are false
}

Example :
let marks = 85;

if (marks >= 90)
{
    document.write("A");
}
else if (marks >= 80)
{
    document.write("B");
}
else if (marks >= 70)
{
    document.write("C");
}
else
{
    document.write("F");
}


4. Nested if-else Statements :

Nested if-else statements in JavaScript are simply if-else statements within other if-else statements. They are used to check for more than one condition and execute different blocks of code based on the combined results of the conditions.

In a nested if-else statement, the inner if-else statement is only executed if the outer if statement is true. If the outer if statement is false, then the inner if-else statement is skipped altogether.

It means, it enters in the first if block, when the 1st condition is true. After that, it checks the inner if block, when the condition of the inner if block is true, then it will return the inner if blocks result, otherwise it returns the inner else block result.
On the other hand's side, when the outer if blocks condition is false, then it will execute the outer else block.

Syntax :
if(condition1)
{
    if(condition2)
    {
        // Code to be executed if the inner if's condition if true.
    }
    else
    {
        // Code to be executed if the inner if's condition is false
    }
}
else
{
    // Code to be executed when the outer if's condition in flase.
}

Example :
Check whether the number is greater than 5 and less than 10 or not.
let num = 7;
if(num > 5)
{
    if(num < 10)
    {
        document.write("Number is Greater than 5 and less than 10");
    }
    else
    {
        document.write("Number is Greater than 5  and 10");
    }
}
else
{
    document.write("Number is less than 5");
}


5. switch statements :

The switch statement allows you to execute different blocks of code based on the value of an expression.
Simply, it executes a block of code as per user's input values of an expression.

Syntax :
switch (expression)
{
  case value1:
    // code to be executed if expression is equal to value1
    break;

  case value2:
    // code to be executed if expression is equal to value2
    break;
    
    ...

  default:
    // code to be executed if none of the cases are true
}

Example :
let day = "Monday"
switch(day)
{
    case "Monday":
    document.write("Today is Monday !")
    break;

    case "Tuesday":
    document.write("Today is Tuesday !")
    break;

    case "Wednesday":
    document.write("Today is Wednesday !")
    break;

    case "Thursday":
    document.write("Today is Thursday !")
    break;

    case "Friday":
    document.write("Today is Friday !")
    break;

    case "Saturday":
    document.write("Today is Saturday !")
    break;

    default:
    document.write("Today is Sunday !")
}


So in this post, we have seen Conditional statements and the use of conditional statements in Javascript.

In the next post, we are going to see, Looping statements in Javascript..!

I hope this article will help you to learn Javascript more..!

Thank You..!!




Also Visit


Post a Comment

0 Comments