JavaScript Control Statements – Conditional Statements
Here, we will learn Control statements – conditional statements in JavaScript.
Control Statements decide according to the condition whether a statement should be executed or not. In this post, we further go into detail about these control statements.
This post is to give a basic introduction to control statements in Javascript. At the end of this article, you will clearly understand the usage of conditional statements.
Types of Control Statements in JavaScript
- Conditional statements
- Loop /Iterative Statements
Let’s understand them with the examples:
- Conditional Statements: if, if-else, else-if ladder, switch
IF Statement
The syntax for if statement in JavaScript.
if(condition){
//block of code 
}example:
<html lang="en">
<head>
 <title>if statement</title>
 <script language="javascript" type="text/javascript" >
 var a = window.prompt("Enter a number", "0");
 a = parseInt(a);
 if(a<100){
 document.writeln("The number is less than 100");
 }
 </script> 
</head>
<body>
</html>
IF-ELSE Statement
The syntax for the if-else statement in JavaScript.
if(condition){
//block of code 
}else{
//block of code 
}example:
<!doctype html>
<html lang="en">
<head>
 <title>if-else statement</title>
 <script language="javascript" type="text/javascript" >
 var a = window.prompt("Enter a number", "0");
 a = parseInt(a);
 var b= parseInt(prompt("Enter b number"));
 if(a > b)
 window.alert("The greatest is " +a);
 else 
 alert("The greatest is " +b); 
 
 </script> 
</head>
<body>
</html>
ELSE-IF Statement
The syntax for the else-if statement in JavaScript.
if(condition1){
//logic 
}else if(condition2){
//logic
}else{
//logic
}example:
<!-- Arranging 3 numbers in order -->
<!doctype html>
<html lang="en">
<head>
 <title>nested if statement</title>
</head>
<body>
 <script language="javascript" type="text/javascript" >
 var a = parseInt(window.prompt("Enter Marks Sub1"));
 var b= parseInt(prompt("Enter Marks Sub2"));
 var c= parseInt(prompt("Enter Marks Sub3"));
 var avg=(a+b+c)/3;
 if(avg >= 70)
    alert("First With Distinction");
 else if(avg >=60 && avg <70)
    alert("First Class");
 else if(avg >=50 && avg <60)
    alert("Second Class");
 else if(avg >=40 && avg <50)
    alert("Third Class");
 else
    alert("Fail"); 
 </script> 
</body>
</htmlSWITCH Statement
The syntax for the switch statement in JavaScript.
switch(expression) {
  case x:
    // block of code 
    break;
  case y:
    // block of code 
    break;
  default:
    // default block 
}example:
<!-- finding the grade using switch -->
<html>
 <head> 
 <title> switch </title>
 </head>
 <script language="javascript" type="text/javascript">
 
 var n1 = parseInt(window.prompt("Enter the marks in Sub1"));
 var n2 = parseInt(window.prompt("Enter the marks in Sub2"));
 var n3 = parseInt(window.prompt("Enter the marks in Sub3"));
 var avg= (n1+n2+n3)/3;
 
 var ch = parseInt(avg/10); //parse to int
 
 switch(ch)
 {
 case 10:
 case 9:
 case 8:
 case 7: window.alert("Distinction");break;
 case 6: window.alert("First Class");break;
 case 5: window.alert("Second Class");break;
 case 4: window.alert("Third Class ");break;
 default: window.alert("Fail ");
 }
 </script>
 </body>
</html>
Leave a Reply