While a loop, an additional loop, although the condition is false

I am using javascript, but I am looking for a general purpose solution that can be applied to multiple languages.

I want the while loop to work one time longer than expected.

For example (suppose the variables are defined above):

while (x != ">") { i++; tempStr += x; x = text[i]; } 

Thus, the code above would have tempStr last character ">" .

It is important to remember that I'm not just trying to do something like this:

 while (x != ">") { i++; tempStr += x; x = text[i]; } tempStr += x; 

The above example is just one example where it would be convenient to start the while loop for one final loop after the condition is false. And that’s all, although I can’t share my actual code with you (for legal reasons), just know that the above will not be the solution for the application that I have in mind.

It may not be possible to do what I am trying to do, if so, let me know :)

+6
source share
6 answers
  while(condition || !extraLoopCondition) { //The usual stuff if(!condition) { extraLoopCondition = true; } } 

Until he’s very eloquent, he’s sure to do what you want

Presumably, the last loop will have condition as false at the end

+4
source

Yes, the do ... while () construct is in most languages.

http://www.php.net/manual/en/control-structures.do.while.php

 <?php $i = 0; do { echo $i; } while ($i > 0); ?> 
+1
source

From Mike's comment, use the do loop:

 var tempStr = ''; var x, i=0; do { x = text[i++]; tempStr += x; } while (x != '>') 

Note that you should probably have an alternative limit to prevent an infinite loop in case x is never a ">", for example.

 ... while (x != '>' && text[i]) 

or similar. Also check that access to string characters by index works in all browsers, some of them do not allow it (older IE), it is probably better to use:

  x = text.substr(i++, 1); 

or

  x = text.charAt(i++); 
+1
source

you say you don’t want the code after that ... as in

 while (x != ">") { i++; tempStr += x; x = text[i]; } tempStr += x; 

but you also say that do{}while does not work, because the condition must be checked .. BUT .. if the condition must be confirmed, it means that you have code outside the loop. but before that. your sample only works if the code is like this

 x = text[i=0]; while (x != ">") { i++; tempStr += x; x = text[i]; } 

so I really don't see the problem with code outside the loop. You cannot avoid it unless you use some kind of unorthodox code, for example

 for(x = text[i=0];(tempStr = tempStr +x).substring(tempStr.length-1) != ">";x = text[i]) i++ 

but this is the worst (uggliest) solution .. no matter what the problem .. in any way .. when I am parsing or string manipulating and I need to add the last char usually my code looks like this

 for(var i=0;text.length>0;i++) { var x = text[i]; //get the char tempStr += x;//do somthing..like..append if(x==">")//is the last one admissible break; } 
+1
source

Actually, I just thought. However, did not check it. (esdebon also suggested this, I just didn't see it before I posted it)

 if (x != ">") { do { i++; tempStr += x; x = text[i]; } while (x != ">") } 
0
source

Does this fit your requirements?

 if( x != ">" ) tempStr += x; while (x != ">") { x = text[++i]; tempStr += x; } 

This will leave the last tempStr character as > if x no longer equal to '>' .

0
source

All Articles