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

7
  • I would say your condition should be tweaked so that when it is false, it doesn't need to be run another time. Commented Feb 26, 2014 at 23:36
  • 5
    Would a do loop work? Commented Feb 26, 2014 at 23:37
  • 2
    You can use an "if" and then the "do loop" Commented Feb 26, 2014 at 23:41
  • 1
    Maybe a do loop wrapped in an if statement with the initial condition? Commented Feb 26, 2014 at 23:42
  • 1
    @esdebon - Heh, you beat me! Commented Feb 26, 2014 at 23:42

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

3 Comments

Shouldn't it be ||?
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?
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?
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);
?>

2 Comments

do...while performs the loop code an extra time at the beginning, not the end
As you can see from the users answer to his own question, that is irrelevant.
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++);

2 Comments

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.
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.
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;
}

Comments

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 != ">")
}

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.