# Thread: while (a++ < 10)

1. ## while (a++ < 10)

Is this a 'bug' I think I have had probs with this and have had to
change it to:-

Code:
```a=0;
While(a<10) {
....
....
a++
}```
The example in the title seemed to skip the case where a=0
and start off with a=1.

I was under the impression that a++ meant the increment
was done before the test as in the asignment:-
Code:
```a=0
b=a++; /* b now 0 */```
but:-
Code:
```a=0;
b=++a; /* b now 1 */``` 2. There are two forms of the increment operator. The prefix and the postfix version. The prefix version is ++a and the postfix version a++. Both have the same result if used by themselves but in when used in statements like the above there is a difference. In your assignment example, b=a++, b is assigned the current value of a and then a is incremented. In b=++a, a is incremented first then the result is assigned to b.

I was under the impression that a++ meant the increment
was done before the test as in the asignment:- 3. Originally Posted by silk.odyssey
There are two forms of the increment operator. The prefix and the postfix version. The prefix version is ++a and the postfix version a++. Both have the same result if used by themselves but in when used in statements like the above there is a difference. In your assignment example, b=a++, b is assigned the current value of a and then a is incremented. In b=++a, a is incremented first then the result is assigned to b.
Yes I know that, but what happens when it is not an assignment
but a test condition situation?? It seems the increment is done
before the test irregardless of ++a or a++. 4. Since you haven't shown the actual body of the while look its hard to know what behavior you are seeing. However lets examine two cases:

Code:
```int a = 0;
while ( a++ < 10 )
printf("%d\n", a);```
In this case it will print the numbers from 1 to 10 inclusivly.

The other case would be:
Code:
```int a = 0;
while ( ++a < 10 )
printf("%d\n", a);```
Which would print the numbers from 1 to 9.

From your example it appears you are using the value of a inside your while loop. If this is true then you don't want to do the increment inside the while condition which is executed before the body of the loop.

A for loop appears to be what you would want to use. 5. Originally Posted by Thantos
Since you haven't shown the actual body of the while look its hard to know what behavior you are seeing. However lets examine two cases:

Code:
```int a = 0;
while ( a++ < 10 )
printf("%d\n", a);```
In this case it will print the numbers from 1 to 10 inclusivly.

The other case would be:
Code:
```int a = 0;
while ( ++a < 10 )
printf("%d\n", a);```
Which would print the numbers from 1 to 9.

From your example it appears you are using the value of a inside your while loop. If this is true then you don't want to do the increment inside the while condition which is executed before the body of the loop.

A for loop appears to be what you would want to use.
Both your example are the same.
However I know the increment is done before the test cos it
fixed a bug. 6. Futher more I think it dates back to machine code where u can have
a=a++ (register a).
There would not be a machhine code to do the increment after the test
which is why I have previously ran into probs.
A weakness in the language I would say. 7. The behavior of this is undefined. Originally Posted by esbo
a=a++ (register a).
http://c-faq.com/expr/ieqiplusplus.html

>Both your example are the same.
No, they are as described.

Code:
```   while ( a++ < 10 )
printf("%d\n", a);```
Compare the current value of a with 10, then increment a and enter the loop.

And this:
Code:
```   while ( ++a < 10 )
printf("%d\n", a);```
Increment the value of a, then compare it with 10 and enter the loop.

In either case, a is incremented before it enters the loop. If a is 0 before the loop, then in either case it will be 1 on the first iteration.

Now,
Code:
```a=0;
while(a<10) {
a++
}```
Do nothing to a, compare to 10, the enter the loop (which increments a).

All three are different. All three produce different results. 8. A weakness in the language I would say.
Why is that? Increment and decrement operations have precedence over comparison operators because they might be part of the test condition. If you tested before doing math it would mess up a lot of logic. 9. Originally Posted by Dave_Sinkula
The behavior of this is undefined.http://c-faq.com/expr/ieqiplusplus.html

>Both your example are the same.
No, they are as described.

Code:
```   while ( a++ < 10 )
printf("%d\n", a);```
Compare the current value of a with 10, then increment a and enter the loop.

And this:
Code:
```   while ( ++a < 10 )
printf("%d\n", a);```
Increment the value of a, then compare it with 10 and enter the loop.

In either case, a is incremented before it enters the loop. If a is 0 before the loop, then in either case it will be 1 on the first iteration.

Now,
Code:
```a=0;
while(a<10) {
a++
}```
Do nothing to a, compare to 10, the enter the loop (which increments a).

All three are different. All three produce different results.
I think the problem with is while ( ++a < 10 ) is that you would
have to set a to one less than the first value you wanted to
process make it work, so in a lot of my code I had to set variables
to -1 to produce the desired result, 0 which is a bit unnatural.

My 'flawed' thinking made me expect the loop would be processed
once before "a" would be incremented.
In future I will make all such loops:-

Code:
```a=0;
while(a<10) {
...
a++
}```
This removes any confusion in my mind as to what is happening
and it should save me a lot of grief debugging code. I would be
nice to have a loop to do what I wanted it to do though. 10. Originally Posted by citizen
Why is that? Increment and decrement operations have precedence over comparison operators because they might be part of the test condition. If you tested before doing math it would mess up a lot of logic.

I think that they are more suited to a computers mind than a
humans, for example I would have to remember the above
statement when reading and writing code!! 11. > nice to have a loop to do what I wanted it to do though.
You've taken a very long road just to invent
for ( a = 0 ; a < 10 ; a++ )

No wait, there's nothing to invent, C does this anyway. 12. Originally Posted by Salem
> nice to have a loop to do what I wanted it to do though.
You've taken a very long road just to invent
for ( a = 0 ; a < 10 ; a++ )

No wait, there's nothing to invent, C does this anyway.
I guess so, I guess I get a little uneasy with the language cos
I would expect:-

Code:
`for ( a = 0 ; a < 10 ; a++ )`
to be similar to:-

Code:
```a=0;
while ( a ++< 10 )```
Because I would expect everything within the brackets to be done
at the same time for consistancy.
Basically I am just to lazy to read the manual Maybe it is because I am used to evaluating mathematical expressions and the for loop 'breaks the rules'.

It is a bit of a case things put in to make things easier for
me have made things harder for me.

Also you can't put debug statements into the for
statement? Then again maybe I would need em if I used
it. 13. Also you can't put debug statements into the for statement?
You can put debugging statements anywhere you can put normal statements. Inside loops, functions, switch statments, if statements, anywhere you want.

This
Code:
```for(a = 0; a < 10; a ++) {
function();
}```
is the same as
Code:
```a = 0;
while(a < 10) {
function();
a ++;
}```
[/edit] 14. Originally Posted by dwks
You can put debugging statements anywhere you can put normal statements. Inside loops, functions, switch statments, if statements, anywhere you want.

This
Code:
```for(a = 0; a < 10; a ++) {
function();
}```
is the same as
Code:
```a = 0;
while(a < 10) {
function();
a ++;
}```
[/edit]

Code:
```for(a=0; function(); a<10; function; a++; function()){

}``` 15. Maybe it is because I am used to evaluating mathematical expressions and the for loop 'breaks the rules'.
I find this funny because Dennis Ritchie is a mathematian and Brian Kernighan is an engineer. So both come from a heavy math based background. Almost all of C's expressions come from a math base.

It might be my mind going but IIRC the for loop wasn't present in the early stages of C. It was added later as a nicer way to package loops up. Popular pages Recent additions 