## Desicion Making

Very often while writing code, there is a need for performing different actions for different decisions. Conditional statements are used to meet those needs. Javascript offers the following conditional statements
if condition to decide to execute a block of code when the specified condition is true.
else condition to decide to execute a block of code when the condition specified in if is false.
else if  to specify a new condition to execute a new block when the first condition is false
switch to jump between many alternative blocks of code based on the requirement

## The if Statement

if condition to decide to execute a block of code when the specified condition is true.

### Syntax:

if (condition) {
block of code to be executed if the condition is true
}

Example

<!DOCTYPE html>
<html>
<body>
<p id="demo">a is greater than b</p>
<script>
var a=10;
var b=20;
if (a<b) {
document.getElementById("demo").innerHTML = "a is less than b";
}
</script>
</body>
</html>

### Output

a is less than b

``` <script> /* JavaScript program to check if the input year is a leap year or not by using following conditions The year can be evenly divided by 4, unless; The year can be evenly divided by 100, it is NOT a leap year, unless; The year is also evenly divisible by 400. Then it is a leap year. Input Enter year: 1992 Output 1992 is a leap year */ msg = 'Enter year:'; var year = prompt(msg); //integer conversion year = parseInt(year) //first divisible by 4 and not by 100 or second divisible by 400 if ((year % 4 == 0 && year % 100!=0) || year%400==0){ document.write(`\${year} is a leap year`);} else{ document.write(`\${year} is a leap year`);} </script> ```

## The if else statement:

The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.

### Syntax:

if (expression){
Statement(s) to be executed if expression is true
}
else{
Statement(s) to be executed if expression is false
}

### Example:

<html>
<body>
<script>
var a=10;
var b=20;
if (a>b) {
document.write("a is less than b");
}
else{
document.write("a is greater than b");
}
</script>
</body>
</html>

Output:
a is greater than b
``` <script> /* JavaScript program to print the grade of person using the following conditions ->A if percentage >85 ->A- if %<=85 and >80 ->B if %<=80 and >70 ->C if %<=70 and >60 ->D if %<=60 and >40 ->E if %<=40 and also should print 'candidate failed' if %<=35 Input enter the percentage of student: 86 Output Grade - 'A' */ //finding grades //message variable var msg = 'enter the percentage of student:'; //printing message for user input var a = prompt(msg); if(a>85) {//if percent > 85 --A document.write('Grade - A');} else if(a<=85 && a>80) {//80<percent80=85 --A document.write('Grade - A-');} else if(a>70 && a<=80) {//70<percent<=80 --B document.write('Grade - B');} else if(a>60 && a<=70) {//60<percent<=70 --C document.write('Grade - C');} else if(a>40 && a<=60) {//40<percent<=60 --D document.write('Grade - D');} else if(a<=40) {//percent<=40 --E if(a<=35) {//failed condition document.write('Grade - E Candidate failed');} else {document.write('Grade - E');}} </script> ```

## The else if Statement

else if statement is used to specify a new condition which will be assesed when the first condition is false.

### Syntax:

if (condition1) {
block of code to be executed if condition1 is true
} else if (condition2) {
block of code to be executed if the condition1 is false and condition2 is true
} else {
block of code to be executed if the condition1 is false and condition2 is false
}

### Example:

<!DOCTYPE html>
<html>
<body>
<script>
var greeting;
var time = new Date().getHours();
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
document.write(greeting);
</script>
</body>
</html>

Good day

## The Switch Statement

switch statement is used to select one of many blocks of code to be executed.

### Syntax:

switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
code block
}
This is how it works:

The switch expression is evaluated once.
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.

### Example:

<!DOCTYPE html>
<html>
<body>

<script>
var i=2;
switch(i){
case 0:
document.write("i equals to 0");
break;
case 1:
document.write("i equals to 1");
break;
case 2:
document.write("i equals to 2");
break;
default:
document.write("i equals to 0 or 1 or 2");
}
</script>
</body>
</html>

i equals to 2