JavaScript Control Statement – If-else, Switch Statement 


In this article, we will learn what is if-else and switch statement in javascript. We will explore the control statement with the example. we will describe where to use and what is the use of if and else statement. Using example we will understand how if and else works. After that, we will take a look at how to use the switch statement. Understanding this two control statement will help to make complicated program solving skill.

If

The if statement is the fundamental Control statement that allows JavaScript to make the decision. More precious, if and else statement allows us to choose conditional Choices. You can use if and else statement where you wanted to execute a statement on condition. Let me first show you the syntax of if and else statement.

If the statement has two forms. The first form is.

First, we just need to Specify if keyword to start the control statement and then in the parenthesis specify an expression. So what is the expression? expression in the condition it may be true or false. in the expression, you just need to specify your condition which is Either true or false.

The same may use for the second form also but here we specify to statement for the if condition. So the second form is.

Now in the Second form which specifies two statement. If expression executes the first statement if the expression equivalent to true otherwise the else keyword will execute the false statement. So if the expression is equal to false then the second statement will execute or if the condition is equal to true then the first statement will execute.

let’s take an example of the first and second form of if statement. So in this example, I will show you how to greet people using the program.

when you run this statement you will get a good morning on the console. In the if expression we specify if time is less than 12  then we will execute good morning on the console. show the if statement will first check the condition in the expression and return true If the time value is less than 12.

show the if statement will return true otherwise return false. But what if we set time 15 it means 3 p.m. then what is the result.

You will not get anything on the console. Because we did not specify any message for the false statement.  if you specify 15 to the time variable then the expression is equal to false. So here we specify a second form of if statement where we just use else and specify our second statement.

If-else

Now when you execute this statement you will get a good afternoon on the console.  Because the condition is equal to false so the if statement will skip the first statement and execute the false statement. We always specify false statement in the else block and true statement in the if block.

Compound Statement

if you wanted to specify more than one statement then you can use a block statement. Sometimes called the compound statement. Using the compound statement you can specify more than two statement in the block.

Compound statement is just a Series of the statement enclosed in the curly braces That is treated by JavaScript as a single unit. To insert more than one statement in the if condition.

So this compound statement will execute to statement so it will print good morning and welcome to Daily tuition on the console. You can also make nested if and else condition using the compound statement.

so be inserted nested if in the if statement. you can put nested if condition in the else statement also. you can also create if and else ladder. To create if and else ladder we will write.

So now when you execute this program you will get different output on different situation. So the, if statement will first check the time, is less than 12 if the statement is true then the statement will execute good morning else it will again check if the time is less than 17 minutes 5 p.m then this will execute the good afternoon message on the console and if the statement is not equal to true then the statement will execute automatically and print good evening.

Switch Statement

If and else statement allows us to specify one or two statement on the expression but using switch statement you can specify the expression with the multiple statements. Suppose if you wanted to specify 4 statement for the specific expression then you can do that using the switch statement. If you use if and else statement to execute more than 4 statement then you need to specify multiple expression to execute more than 4 statement. Using the switch statement you can execute multiple statements with the single expression.

let’s take an example of the switch statement.

Using this syntax you can create multiple statements with a single expression. we start switch statement using switch keyword and specify the expression in the parenthesis. Then we open the compound statement using open and close curly braces and in this blog specify our first case and its value. JavaScript will Evaluate expression and pick the case matches to that expressions. So the statement will execute one by one at a time and execute matches case statement and after that exit from the switch statement using break keyword. we talked about this break continue Return throw into future lectures. But just for now keep in mind that the break statement used to break the execution of the statement.

You can specify the number of N values in the case it’s up on you.

Example

Suppose you have a question with four options. you need user input to select one of the four options. when the user selects the option we display that option on the console. This is the very simple example to understand. So if you have a question like

Which language do you choose? that have four options

  1. JavaScript, 2) Python, 3) PHP, 4) Asp.net

and when the user selects one of the four options Then we display that selected option on the console.

Now when you execute this code You will get the first statement on the console. Because we selected the first option, Now if I change the answer variable value and select the second option then the Result would be the second statement and the same thing work for the third and fourth options also. it’s depend on your cases. we just specify 4 cases here you can specify more than that. so switch statement is not Limited for the specific program you can use this concept to achieve the different task.

Now, what if I specify 5 in the answer variable. So switch statement execute default block for those values which was not matched to the cases.

So that is all for this article, we will see you in the next one.