mmm
mmm

Reputation: 2297

While loop, extra loop even though condition is false

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

Upvotes: 6

Views: 7586

Answers (6)

CaldasGSM
CaldasGSM

Reputation: 3062

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

Upvotes: 1

Matt
Matt

Reputation: 20786

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

Upvotes: 0

mmm
mmm

Reputation: 2297

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

Upvotes: 0

RobG
RobG

Reputation: 147383

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++);

Upvotes: 1

krowe
krowe

Reputation: 2270

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);
?>

Upvotes: 1

user70585
user70585

Reputation: 1417

    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

Upvotes: 4

Related Questions