DEV Community

R.Shobika CSE
R.Shobika CSE

Posted on

CONDITIONAL STATEMENT PART - 2

  1. NESTED IF - STATEMENTS: If you have if statement inside if statement is called nested if.

SYNTAX:

if (condition){
---------
if(condition){
--------
}
}

Enter fullscreen mode Exit fullscreen mode

Example Program:

let mark = 90;
let attendance = 75;
if (mark>=85){
if (attendance>=80){
console.log("pass");
}
}
else{
console.log("fail");
}
Enter fullscreen mode Exit fullscreen mode

output: pass

  1. SWICTH STATEMENT

The switch case statement in JavaScript is also used for decision-making purposes. In some cases, using the switch case statement is seen to be more convenient than if-else statements.

Example Program:

let value=4;
switch(value){
case 1:
console.log("FAN");
break;
case 2:
console.log("TV");
break;
case 3:
console.log("AC");
break;
case 4:
console.log("LIGHT");
break;
default:
console.log("There is no switch");
break;
}

Enter fullscreen mode Exit fullscreen mode

output: LIGHT

(If we doesn't give the break for each end of the cases means it will print all the result until when there is a break )

Example Program:

let value=2;
switch(value){
case 1:
console.log("FAN");
break;
case 2:
console.log("TV");

case 3:
console.log("AC");

case 4:
console.log("LIGHT");

default:
console.log("There is no switch");
break;
}
Enter fullscreen mode Exit fullscreen mode

output :TV
AC
LIGHT
There is no switch

Incase we give the value 5 means it will display the there is no switch because there is no case 5 there is only case 1 to case 4 so it will display the default .

In some cases if we doesn't give the default and that time also we give the value 5 means it will display the undefined because there is no default .

TERNARY OPERATOR:

The Ternary Operator is a shortcut for the Conditional statement.

SYNTAX:

condition? true:false
Enter fullscreen mode Exit fullscreen mode

Example Program:

let mark = 45 
let result = mark>=35?"pass":"fail"
Enter fullscreen mode Exit fullscreen mode

output: pass

Reference:(http://geeksforgeeks.org/javascript/control-statements-in-javascript/)

Top comments (0)