Control Statements in JavaScript

The blog describe the usage of Control Statement ( if ..else, Switch , loop) in JavaScript code. The control statements are useful when the code is required to be executed when a particular condition is met or justified or to be executed in loop for a particular interval or execution of code from available expressions. 

JavaScript Control Statement Types

The control statements in JavaScript can be broadly classified into 3 types

  • If -else statements
  • Switch statements
  • Loop

If Statement usage in JavaScript

The if-else statements in JavaScript is required when the code is requried to be executed when a conditon is either true or false. The if statements can be divided into 3 types

  • If statement : executes if the given expression is True
  • If-else statement : executes the first statement if the given expression is True, else the second statement
  • If-else-if statement : executes the statement where the expression is True from the given multiple condition

Syntax for If statement

if(expression1){  
//code executes when if expression1 is true  
}  
else if(expression2){  
//content to be evaluated if expression2 is true  
}  
else if(expression3){  
//content to be evaluated if expression3 is true  
}  
else{  
//content to be evaluated if no expression is true  
}  
<script>  
var x=100;  
if(x==98){  
		document.write("The value of x is 98");  
}  
else if(x==105){  
		document.write("The value of x is 105");
}  
else if(x==100){  
	document.write("The value of x is 100");
}  
else{  
	document.write("The value of x is other than 98,100 or 105");
}  
</script> 

Switch Statement usage in JavaScript

The switch statement is to be used when only one condition is true among the given multiple expressions.

Syntax for Switch statement

switch(expression){  
case value1:  
 //code executes when value1 is matched with expression  
 break;  
case value2:  
 //code executes when value2 is matched with expression    
 break;  
......  
  
default:   
 //code executes when No Value is matched with expression    
}  
<script>  
var color ="Red";
var setColorValue;  
switch(color){  
case 'Blue':  
setColorValue =" The selected color is Blue";
break;  
case 'Orange':  
setColorValue =" The selected color is Orange";  
break;  
case 'Red':  
 setColorValue =" The selected color is Red";
break;  
default:  
setColorValue =" The given color is other than Red, Blue or Orange"; 
}  
document.write(setColorValue);  
</script>  

Loop Statement usage in JavaScript

The Loop statements are useful when required when the program is to be iterated using for, while, do while or for-in loops. The Loop statements are classified into 4 types:

  • for loop : used when the element in the program is to be iterated for fixed number of times
  • while loop : used when the element in the program is to be iterated for infinite number of times till the expression or given condition is met.
  • do-while loop : similar to while loop for infinite number of times till the expression or given condition is met or not. The do while loop executes at least for 1 iteration
  • for-in loop: used to iterate the properties of an object
// example for   For Loop
<script>  
for (x=10; x<=20; x++)  
{  
document.write(x + "<br/>")  
}  
</script> 
//example for While loop
<script>  
var x=10 ;
while (x<=20)  
{  
document.write(x + "<br/>");  
x = x +5;  
}  
</script>  
//example for   do..while loop
<script>  
var x=10;  
do{  
document.write(x + "<br/>");  
x++;  
}while (x<=20);  
</script>