If Else & Switch

Below is the list of conditional statements –

StatementDefinitionSyntax
IfThe if statement is used to test a condition. It executes a block of code if a specified condition is true. if(condition){
//block of code
}
ElseThe else statement executes another block of code, if the same condition is false. Else statement is optional.if(condition){
//block of code
} else {
//another block of code
}
Ternary operatorThe ternary operator consists of a condition that evaluates to either true or false , plus a value that is returned if the condition is true and another value that is returned if the condition is false.variable = (condition) ? expressionTrue : expressionFalse;
SwitchThe switch statement selects one of the blocks of code to be executed. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.switch(expression) {
case 1:
//block of code
break;
case 2:
//block of code
break;
default:
//block of code
}
Scroll to Top