JavaScript Exit for Loop

JavaScript is one of the best programming language that is mostly used by the developers to develop different websites.
The code here is used and delivered along with the web pages directly at the user side and it does not run at the server.
There are a number of features and functions of the language that are used by the programmers to write the JS script so that they can use it immediately and run the code in a good way.
Loop in the JS is used when the programmer has to show the continuous function.
The loop itself has two important functions or the statement the break statement or the continue statement.
img

The break Statement

The break statement or the condition of the loop immediately breaks the loop and starts the execution of the next statement that is written after the loop. It also happens when there is such a condition in the loop that does not fill or complete its parameters.

public void Num( int num1, int val )
{
  for ( int s = 0 ; s < num1 ; s++ )
  {
      for ( int p = 0 ; p < num1 ; p++ )
      {
         if ( s + j >= 2 * val )
            break;
         val = val / 2 ;
      }
    }
}

The continue Statement

The continue statement of the loop depends upon the starting conditions of the loop.
If it fulfills the condition, the loop starts again and continue till until the condition of the loop becomes false. Till then, the new code or the pre-written script does not work.

while ( s != 5 )  // continue the jumps here
{
   if ( s > num )
      continue; // Jumps to condition, s != 5
   System.out.println( "What are you doing?!" ) ;
}

How JS exit for the loop function works?

No matter what is the condition of the function, the loop keeps on iterating the values.
At the end when the loop values become false and not able to fulfil the condition anymore.
The loop function exits and in the next step the next code runs.
The loop does not work until there is a new value in the predefined parameters of the loop.
You can also practice the loop codes before writing the final one.
There are a number of examples from where you can get guidelines and write your own code.

AUTHOR

Yeah, good point, this is a boring problem in for...loops in js. But to solve this problem we can use recursive functions instead of for...loops, it could be a good solution in some cases

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?