Home > other >  Break if statement inside infinite loop
Break if statement inside infinite loop

Time:07-28

This may be an elementary question, but it is breaking my brain.

I have a structure like this in NodeJS (typescript):

while(true) {
   if (something) {
      code...
      try{
         dangerousCode();
      }catch(e){
         console.log(e);
         //Now I want to exit from this if statement and go to the next one
      }
      some other code...
   }
   if (somethingElse) ...
}

How can I leave a catch statement like that and go to the next if (so I need to currently leave the if statement where the catch block is). Should I use break or continue keyword? Should I do something else?

CodePudding user response:

try something like this

while(true) {
   if (something) {
      code...
      try{
         dangerousCode();
         some other code...
      }catch(e){
         console.log(e);
         // Now you will exit this if block, as there are no more instructions
      }
   }
   if (somethingElse) ...
}

If an exception is thrown, it will get caught and handled in the catch block and then directly go the next if statement.

If no exception occurs, the dangerousCode() will execute normally and pass to some other code... after it.

CodePudding user response:

You will have to add another variable and a condition in this case.

let enterFirstIfBlock = true;

while(true) {
   if (enterFirstIfBlock && something) {
      code...
      try{
         dangerousCode();
      } catch(e) {
         console.log(e);
         enterFirstIfBlock = false;
      }
      if (enterFirstIfBlock) {
        some other code...
      }
   }
   if (somethingElse) ...
}

CodePudding user response:

while(true) {
   if (something) {
      code...
      try{
         dangerousCode();
      }catch(e){
         console.log(e);
         //Now I want to exit from this if statement and go to the next one
      }
      some other code...
   }
   if (somethingElse) ...
}

I suggest you move your second if statement to a function say doSomething(), and then call this function within your catch block, and after first if, like this:

while(true) {
       if (something) {
          code...
          try{
             dangerousCode();
          }catch(e){
             console.log(e);
             doSomething();
            continue;
             //Now I want to exit from this if statement and go to the next one
          }
          some other code...
       }
       doSomething();
    }
    function doSomething() {
       if (somethingElse) ...
    }

You will need to pass appropriate params, and handle them accordingly, and also add continue in after doSomething in catch if nothing else is required.

CodePudding user response:

An esoteric, but occasionally convenient feature is using a label with a block scope.

label

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

It allows you to break out of a code block by its label name and continue execution after the end of the block. Here's a contrived example to demonstrate how to use it. (Of course, see the linked documentation as well.)

function fork () {
  return Math.random() < 0.5;
}

function getValue () {
  const useFirst = fork();
  if (fork()) return useFirst ? 'a' : 'b';
  return useFirst ? 1 : 2;
}

while (true) {
  let value = getValue();

  stringCheck:
  if (typeof value === 'string') {
     // code...
     try{
        if (fork()) throw new Error('Bad luck');
     }
     catch (ex) {
        console.error(ex);
        break stringCheck; // Breaks out of the block associated with the label
     }
     // some other code...
  }

  value = getValue();

  numberCheck:
  if (typeof value === 'number') {
    console.log('Got a number');
    // break numberCheck; // If uncommented, would break out of this block
    break; // Break out of the while loop
  }
}

  • Related