"Break" or "Return false"?

Asked

Viewed 1,249 times

4

Within a function there is a repeating structure, as to the processing economy, which is the best?

Javascript

function minhaFuncao(boom){
    for(i = boom; i > 10; i--){
        if(i < 0){
            break;
        }
    }
}

or

function minhaFuncao(boom){
    for(i = boom; i > 10; i--){
        if(i < 0){
            return false;
        }
    }
}

Is there any other even more economical way?

  • 5

    It depends. If you"and just want to interrupt the loop, break is the best option. If you want to interrupt the loop And already abort any function logic, returning a value return is better because it works on both ends.

  • 3

    But... that code will never enter if!

  • The boom can reach less than 10.

  • 2

    In that case the body of for does not perform any time.

2 answers

2


There are 3 options that depend a little on what you want. Note that your code never enters the if for the condition within the for does not allow negative values. Your for run for the last time when the i is 1 and making i-- gives it one last value of 0 and its ifsearching for <0. Having said that the options are:

break;

The break stop the loop and goes to the first line of code afterward loop. If you don’t need to traverse all iterations of a loop but need to run code after loop, use this option.

continue;

continue jumps to the next loop iteration. If you don’t need to run all the code for a specific iteration, but you need all the iterations, use this option.

Return;

Return stops the function it is in and returns the value after the word "Return". Be inside a loop, switch or other function stops immediately and does not run the next line to Return.


Regarding the processing economy depends on what you need. The most economical is Return because it is the most powerful and ensures that no more code is run. The suggestion is to always use the most defective and not limit the code you want to run.

It is useful to use these methods to save processing? Yes, without a doubt.

2

The break in the case of for will serve to interrupt the loop of for, thus by the most ideal passed function. If after the is has code it will be executed normally, that is, only the loop will be stopped. In this case it will not imply anything in processing, because, the loop is interrupted.

The Return false for the full execution of its code and consequently the processing.

Example: Demo

function Imprimir(p) {
    var texto = ""
    var i;
    var demo = document.getElementById("demo");
    for (i = 1; i <= 10; i++) {        
        if (i == 6) {
            if (p === 1) 
            { 
                break; 
            } 
            else 
            { 
                return false;
            }
        }
        texto += "Numero: " + i + "<br>";
    }   
    demo.innerHTML += texto + "<br>";
}
Imprimir(1);
Imprimir(1);
Imprimir(2);

The first two will be printed, because it was used break while the latter it for the execution of the function neither displays result.

  • 1

    Yes, but implies some difference in processing?

  • @Patrick I edited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.