最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - While loop, extra loop even though condition is false - Stack Overflow

programmeradmin4浏览0评论

I'm using javascript, but I'm looking for a general purpose solution that might apply to multiple languages.

I want to have a while loop that runs one time longer than it's supposed to.

For instance (assume the variables are defined above):

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

So the output of the above code would have tempStr's last character be ">".

The important thing to remember, is that I'm not simply looking to do something like this:

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

The above is just one example where it might be convenient to run the while loop for one final cycle after it's condition is false. And all though I can't share my actual code with you (for legal reasons), just know that the above wouldn't be a solution for the application I have in mind.

It might not be possible to do what I'm trying to do, if so, let me know :)

I'm using javascript, but I'm looking for a general purpose solution that might apply to multiple languages.

I want to have a while loop that runs one time longer than it's supposed to.

For instance (assume the variables are defined above):

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

So the output of the above code would have tempStr's last character be ">".

The important thing to remember, is that I'm not simply looking to do something like this:

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

The above is just one example where it might be convenient to run the while loop for one final cycle after it's condition is false. And all though I can't share my actual code with you (for legal reasons), just know that the above wouldn't be a solution for the application I have in mind.

It might not be possible to do what I'm trying to do, if so, let me know :)

Share Improve this question edited Feb 26, 2014 at 23:55 mmm asked Feb 26, 2014 at 23:35 mmmmmm 2,2973 gold badges27 silver badges44 bronze badges 7
  • I would say your condition should be tweaked so that when it is false, it doesn't need to be run another time. – Corey Ogburn Commented Feb 26, 2014 at 23:36
  • 5 Would a do loop work? – Mike Christensen Commented Feb 26, 2014 at 23:37
  • 2 You can use an "if" and then the "do loop" – esdebon Commented Feb 26, 2014 at 23:41
  • 1 Maybe a do loop wrapped in an if statement with the initial condition? – Mike Christensen Commented Feb 26, 2014 at 23:42
  • 1 @esdebon - Heh, you beat me! – Mike Christensen Commented Feb 26, 2014 at 23:42
 |  Show 2 more ments

6 Answers 6

Reset to default 4
    while(condition || !extraLoopCondition) {
        //The usual stuff
        if(!condition) {
            extraLoopCondition = true;
        }
    }

While it's not very eloquent, it will definitely do what you want it to

Presumably the last cycle will have condition to be false at its end

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

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

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

From Mike's ment, use a 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 endless loop just in case x is never ">", e.g.

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

or similar. Also test that accessing string characters by index works in all browsers, some don't allow it (older IE) so probably better to use:

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

or

    x = text.charAt(i++);

you say you don't want code after the while... as in

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

but you also say the do{}while doesn't work because the condition must be validate.. BUT.. if the condition must be validated means that you will have code outside the loop.. but before.. 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 of having code outside the loop.. you cant avoid it unless you do some unorthodox code like

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

but that is the worst(uggliest) solution.. no matter what the problem.. any way.. when I doing parsing or string manipulation and i need to append the last char usually my code goes 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;
}

Actually, I just thought of something. Haven't tested it yet though. (esdebon also suggested this, i just didn't see it before I posted this)

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

Is this consistent with your requirements?

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

This will leave the last character of tempStr as > if x was not already equal to '>'.

发布评论

评论列表(0)

  1. 暂无评论