while ..until


 
Thread Tools Search this Thread
Top Forums Programming while ..until
# 1  
Old 09-19-2002
while ..until

how do u say this :

decrement the pointer chr untill you find chr == ' ' OR chr == '<'
# 2  
Old 09-20-2002
do
{
chr--; // or *chr-- whatever is reqd.
} while(chr != ' ' && chr !='<');
# 3  
Old 09-20-2002
OR

Sorry,

but he said chr== ' ' OR chr == '<'. And you say && which means AND, therefore you will never find a char which is both: ' ' AND '<'.

I think you have to use || which means OR instead of && (= AND)!

Also if you use "do { .. } while(..)" , the pointer gets decremented and afterwards compared to the while statement. Therefore you loose the first character.

Better use:

while(chr != ' ' || chr !='<') {
chr--; // or *chr-- whatever is reqd.
}

Here the value of the pointer gets compared first and then decremented!


hope I am right,

have fun
darkspace
# 4  
Old 09-20-2002
Re: OR

Quote:
Originally posted by darkspace
Better use:

while(chr != ' ' || chr !='<') {
chr--; // or *chr-- whatever is reqd.
}
You will stay in that loop while:
chr is not a space OR
chr is not a less than sign

One of those will always be true. So you have an infinite loop, now.

The original answer was better, but both seem to be using chr as both a pointer to character and as a character.

I don't like the do/until concept, but that is what the OP requested. For some reason, he is sure that the loop must be executed at least once.

And also, when scanning a string, you really need to check for the end of the string. I know that the OP didn't request this, but I can't help myself on this one.

So my answer:
Code:
do {
    chr--;
} while (*chr && *chr != ' ' && *chr != '<');

# 5  
Old 09-30-2002
Re: Re: OR

Totally I agree with Perderabo's opion.

But if the original value of chr is pointing to ' ' or '<', then
his answer will loss it because he uses do...while structure.

I think while structure is better.

This is my answer:
while ( *chr && *chr != ' ' && *char != '<'){
chr--;
}

I hope it be right.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question