Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - CONTINUE


Continue statement is used with the loops to skip a few lines of code below it.


Let us make it simple with the below example.


Continue with for loop


Say we have a for loop that prints all the letters of a String (i.e. CAT).


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	for (x of "CAT") {    
		document.write(x, ", ") 
	}        
    
</script>      
</body>
</html>


Output :



  C, A, T,

Now, let's say, we don't want the letter A to be printed. We just want to document.write C and T as output.


Let us rewrite the program using continue statement.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	for (x of "CAT") { 
    	if (x == 'A') {
			continue
        }    
		document.write(x, ", ") 
	}        
    
</script>      
</body>
</html>


Output :



  C, T,

Now, if you see the output, the space A is omitted from the output. Printing only C and T.


Let us see it in detail :


So, we have a for loop that Iterates through the letters CAT.


for (x of "CAT")

And the Iteration starts,


1st Iteration


In the 1st Iteration, C is taken and stored in the variable x.

java_Collections

So, in the next line, we check if the value of the variable x is A or not.


if (x == 'A') {
	continue
}

In this case the value of x is C. So, we do not enter the if block and come to the document.write statement.


document.write(x, ", ")

Printing the value of x.

Output :



  C,

Then we start the second Iteration of for loop.


2nd Iteration


In the 2nd Iteration, A is taken and stored in the variable x.

java_Collections

So, in the next line, we check if the value of the variable x is A or not.


if (x == A) {
	continue
}

In this case the value of x is A. So, we enter the if block and come to the continue statement.


continue

And what the continue statement does is, ends the current Iteration of the for loop and starts the next Iteration.

java_Collections

And the value of x i.e. A is not printed..


And the 3rd Iteration starts.


3rd Iteration


Similarly, in the 1st Iteration, T is taken and stored in the variable x.

java_Collections

Same way, in the next line, we check if the value of the variable x is A or not.


if (x == 'A') {
	continue
}

In this case the value of x is T. So, we do not enter the if block and come to the document.write statement.


document.write(x, ", ")

Printing the value of x.

Output :



  C, T,

With this we end the execution of for loop.


Next, let us see another example of continue using the while loop.


Continue with while loop


Say you have a while loop that is used to document.write the numbers from 1 to 4.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 1 
	while (x <= 4) {
		document.write(x, ", ")
		x++
    }    
    
</script>      
</body>
</html>


Output :



  1, 2, 3, 4,

But let us say, you do not want to print the value 3.


i.e. You just want to document.write the numbers 1, 2 and 4.


And we can achieve it using continue statement.


Let us modify the above program using the continue statement.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 1 
	while (x <= 4) {
    	if (x == 3) {
			x++	
			continue
        }    
		document.write(x, ", ")
		x++
    }    
    
</script>      
</body>
</html>


Output :



  1, 2, 4,

And all we have done is placed an if condition, right before the document.write statement, checking if the value of x is equal to 3.


if (x == 3) {
	x++
	continue
}

And the continue statement helps to skip the below lines of the while loop.


document.write(x, ", ")
x++

Just remember if the value of x is equal to 3. Only then continue statement executes skipping the document.write statement and the increment statement (i.e. x = x + 1).

java_Collections

And the value 3 is not printed.