Conditional statements

JavaScript Conditional statements are used to perform different actions based on different conditions..

In JavaScript we have the following conditional statements:

1) Use if to specify a block of code to be executed, if a specified condition is true
2) Use else to specify a block of code to be executed, if the same condition is false
3) Use else if to specify a new condition to test, if the first condition is false
4) Use nested if-if statements inside if statements
5) Use switch to specify many alternative blocks of code to be executed

IF-Statement

if check the condition -if condition is true then print something otherwise print nothing.

Syntax:-

if(condition)
{
statement
}

IF-Example

<html>
<head>
<title>if-statement</title>
<script>
var a;
a=10;
if(a>10)
{
document.write("a is greater than 10");
}
</script>
</head>
<body>
</body>
</html>

JavaScript If...else Statement

If else statement check condition -if condition is true then print something(true statement) otherwise print something(false statement).

Syntax:-

if(condition)
{
statement
}
else
{
statement
}

IF ELSE-Example

<html>
<head>
<title>if else-statement</title>
<script>
var a;
a=10;
if(a>10)
{
document.write("a is greater than 10");
}
else
{
document.write("a is smaller than 10");
}
</script>
</head>
<body>
</body>
</html>

JavaScript elseif Statement

When you have a multiple conditions use it elseif statement. elseif statement check condition -if condition is true then print something(true statement) otherwise check next condition is it true or not.

Syntax:-

if(condition)
{
statement
}
elseif(condition)
{
statement
}
elseif(condition)
{
statement
}
else
{
statement
}

ELSEIF-Example

<html>
<head>
<title>elseif-statement</title>
<script>
var age;
age=parseInt(prompt("Enter your age"));
if(age>18)
{
document.write("You are elligible for vote");
}
else if(age==18)
{
document.write("You are also elligible for vote");
}
else
{
document.write("You are not elligible for vote");
}
</script>
</head>
<body>
</body>
</html>

JavaScript Nested if statement

When you have a multiple conditions use it nested if statement. Nested if means if inside another if.if first condition is true than check second condition is true or not.

Syntax:-

if(condition)
{
      if(condition)
      {
      statement
      }
      else
      {
      statement
      }
}
else
{
statement
}

NESTED IF-Example

<html>
<head>
<title>elseif-statement</title>
<script>
var age;
var nat;
nat=prompt("Enter your Nationality");
age=parseInt(prompt("Enter your age"));
if(nat=="indian")
{
      if(age>18)
      {
      document.write("You are elligible for vote");
     }
      else
      {
      document.write("You are not greater than 18");
      }
}
else
{
document.write("You are not an indian");
}
</script>
</head>
<body>
</body>
</html>

IF WITH LOGICAL (&&) OPERATOR

<html>
<head>
<title>if-statement</title>
<script>
var a,b,c;
a=parseInt(prompt("Enter first number"));
b=parseInt(prompt("Enter second number"));
c=parseInt(prompt("Enter third number"));
if(a>b && a>c)
{
document.write("A is greater than b and c");
}
else if(b>a && b>c)
{
document.write("B is greater than a and c");
}
else
{
document.write("C is greater than a and b");
}
</script>
</head>
<body>
</body>
</html>

IF WITH LOGICAL (||) OPERATOR

<html>
<head>
<title>if-statement</title>
<script>
var ch;
ch=(prompt("Enter a character"));
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
document.write(ch," is a vowel");
}
else
{
document.write(ch," Not a vowel");
}
</script>
</head>
<body>
</body>
</html>

Switch case

A program becomes quite difficult to understand when there are multiple if statements.To simplify coding and to avoid using multiple if statements,switch case statement can be used as a different approach to code the same logic.The switch case statement allows comparing a variable or expression with multiple values.

Syntax

switch (expression)
{
      case condition 1: statement(s)
      break;
      case condition 2: statement(s)
      break;
      ...
      case condition n: statement(s)
      break;
      default: statement(s)
}

switch:

Executes a specific case statement that holds the value of the expression or the variable.

case:

A value and a colon follow the case keyword.The block of a specific case statement is executed when the value of switch expression and the case value are the same.Each case block must end with break keyword.

break:

Passes the execution control to the statement existing immediately out of the switch-case statement.if there is no break statement,the next case statement is executed.

default:

The execution control passes to the default bloick when none of the case values matches the switch expression.the default block is same as the else block.

Switch-case Example

<html>
<head>
<title>switch cse</title>
<script>
var grade;
grade=prompt("Enter a character");
switch(grade)
{
case 'a':
   document.write("Excellent");
break;
case 'b':
   document.write("Very Good");
break;
case 'c':
   document.write("passed");
break;
case 'D':
   document.write("Not so good");
break;
case 'F':
   document.write("Failed");
break;
default:
   document.write("Unknown grade")
}
</script>
</head>
<body>
</body>
</html>

Assignment

1. Write a JavaScript program that displays the largest integer among two integers.
2. Write a JavaScript conditional statement to find the sign of the product of three numbers. Display an alert box with the specified sign.
Sample numbers : 3, -7, 2
3. Write a JavaScript conditional statement to find the largest of five numbers. Display an alert box to show the results.
Sample numbers : -5, -2, -6, 0, -1
4. Write a JavaScript program that computes the average marks of the following students. Then, this average is used to determine the corresponding grade.

Student Name	Marks
David    	 80
Vinoth	         77
Divya	         88
Ishitha	         95
Thomas	         68
The grades are computed as follows :

Range	Grade
<60	    F
<70	    D
<80	    C
<90	    B
<100	    A