6

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 :)

mmm
  • 2,272
  • 3
  • 25
  • 41

6 Answers6

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

user70585
  • 1,397
  • 1
  • 14
  • 19
  • 1
    Shouldn't it be `||`? – zerkms Feb 26 '14 at 23:41
  • Would it be more performant to use a while loop with an `||` and the if statement inside, or to have the if statement outside and a do while in the if statement? – mmm Feb 26 '14 at 23:50
  • I thought you said in the OP that you couldn't have the code to run outside of the while loop. If that's not the case, what's wrong with the second thing you posted there? – user70585 Feb 26 '14 at 23:53
1

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);
?>
zerkms
  • 249,484
  • 69
  • 436
  • 539
krowe
  • 2,129
  • 17
  • 19
1

From Mike's comment, 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++);
RobG
  • 142,382
  • 31
  • 172
  • 209
  • A do loop wont work because it'll execute once even if the condition is false. I'm trying to only begin the loop when the condition is met, and then run one extra time at the end. – mmm Feb 26 '14 at 23:48
  • Then you should specify that in the question. You need two tests—one to initiate the loop and one to stop it. There is no *if..while* loop, but you can nest a *while* inside an *if*. – RobG Feb 27 '14 at 00:42
1

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;
}
CaldasGSM
  • 3,032
  • 16
  • 26
0

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 != ">")
}
mmm
  • 2,272
  • 3
  • 25
  • 41
  • If you select your own answer now that is so lame. 10 people here knew exactly what to do. You just never selected it until it answered part of your problem which wasn't clear in the question. – krowe Feb 27 '14 at 00:00
0

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 '>'.

Matt
  • 20,108
  • 1
  • 57
  • 70