Home > Software engineering >  How to take break out of for loop
How to take break out of for loop

Time:06-21

I want to take if condition out of for loop to resolve cyclomatic complexity. How can I take out the break statement out of for loop?

here is the sample code. abc2 was the original function

function abc2(){
for(var i=1; i<8; i  ){
if(i==5)
break
console.log(i)
}
}

function abc(){
for(var i=1; i<8; i  ){
aa(i);
console.log(i)
}
}

function aa(i){
if(i == 5)
break; // how to use break here
}
<html>
<body>

<button type="button"
onclick="abc()">
Click.</button>

</body>
</html> 

CodePudding user response:

You can refactor the aa(i) function to return a boolean. The break statement breaks out of a switch or a loop which is not the case you showed above, hence you're receiving the error.

function abc(){
  for(var i=1; i<8; i  ){
    if (aa(i)){
        console.log(i)
        break;
    }
  } 
}

function aa(i){
  return i == 5;
}

abc();

CodePudding user response:

function aa(i){ if(i == 5) break; // how to use break here }

this above part is only an if statement, you cannot break from an if statement since there is no any loop to break from. break can be only used in a loop.

refer the below link fro more info

i hope you will find your answer

https://stackoverflow.com/questions/24714287/break-out-of-if-statement#:~:text=breaks don't break if statements.&text=But break s don't,phone calls over nine hours.&text=You can't break out,if is inside a loop.

CodePudding user response:

From a morre abstract perspective, you intend to write a function where you affect the program flow in the calling context.

Other than returning a value from the function or setting some state accessible from the context (ie. producing side effects) this will not work - and rightly so as it violates basic best practice of SW engineering (namely encapsulation and locality) and is guarantueed to entail code hard to reason about and to maintain. Note that many seasoned developers already frown upon side effects.

What you can do is changing the loop bound;

the loop into two or modifying the increment operation:

function abc(){
    for(var i=1; i<5; i  ){
        aa(i);
        console.log(i)
    }
    // Now handle special case i == 5. Higher values of i will not be reached with the original loop definition
}

Maybe you intend to skip i==5in the original loop. You should split the loop into 2:

function abc(){
    for(var i=1; i<5; i  ){
        aa(i);
        console.log(i)
    }
    for(var i=6; i<8; i  ){
        aa(i);
        console.log(i)
    }
}

You might want to generalize that into some function if you apply thes pattern frequently.

Alternatively, modify the incerement:

function abc(){
    for(var i=1; i<8; i = (i === 4) ? (i 2) : (i 1)){
        aa(i);
        console.log(i)
    }
}

CodePudding user response:

I am not sure if it is possible to take out if because it is a condition and you need to exclude that condition either with an if or some other conditional. Maybe like a do/while.

In case you want only to skip the value of 5, you can use this:

function abc2(){
      for(var i=1; i<8;) {
        if(i==5){
        // don't run any code here, just increment i
         i  ;
        } else {
        someFunction() // run whatever code you want
        i  ; // then increment i
        }
      }
    }
  • Related