The Switch Statement:

In above topic you learn that how you can conditionally run a programme and you can give multiple conditions in “else if” statement. But it is not a good practice to do that for multiple conditions. In JavaScript we have a conditional statement “switch” to do that.

Syntax:

The basic syntax for Switch statement is as follows

In above syntax you can see that there is an expression which means the condition which you want to perform in your programe. The cases in switch statement are used to execute the statements written in case if the condition is true if all the conditions are false than the default condition will execute.

Example:

In following example we are giving a true condition to execute

Example:

In following example we are giving a false condition to execute

Break Statement in Switch:

The “break” statement is very important in Switch because the break statement is use to terminate the condition if it is true and the other statements will not execute. If you skip the break statement the all statements will executed after the case which is conditionally true.

Example:

By following example you understand it easily.

 

Leave a Reply

Your email address will not be published. Required fields are marked *