Conditional Statements:
Conditional Statements are those statements which run different block of codes according to the condition or we can say these are those statements which perform different actions.
Following conditional statements are used in javascript:
- if statement
- if else statement
- else if statement
- switch case
if Statement:
It runs a block of code which is written inside it if condition is true.
if (expression){
code to be execute
}
if else Statement:
In this if condition is true then code written inside if block will execute and if condition is false then code written inside else block will execute.
if (expression){
code to execute if expression is true
}
else{
code to execute if expression is false
}
else if Statement:
In this if condition is true then code written inside if block will execute and otherwise whose condition is true that else if code block will execute and if condition is still false then else block code will execute.
if (expression 1){
code to execute if expression 1 is true
}
else if (expression 2){
code to execute if expression 2 is true
}
else if (expression 3){
code to execute if expression 3 is true
}
else{
code to execute if no expression is true
}
switch case:
In switch case expression is given inside switch and there are multiple cases and that case run whose expression matches with the main conditionn. If there is no match then default block runs.
switch (expression)
{
case condition 1: set of statements to execute
break;
case condition 2: set of statements to execute
break;
case condition N: set of statements to execute
break;
default: set of statements to execute
}
0 Comment(s)