# Ramucho25

EstablishedMember

6

0 Neutral

• Rank
Newbrie
1. ## "for" Loop Infinite

I think for almost everybody else the code does exactly what they would expect.x is of type char (8 bit data), therefore its value can never exceed 255, therefore the loop never terminates. Regards Dave Yes, for 99.9% it's ok if you don't have to use the extreme values of the variable used in a for loop (0 and 255) . As you're saying, an 8 bit variable can take 256 values from 0 to 255, I know that. So, why do you speak of values over 255 ? There's nothing like that written in the C code. You're probably speaking of how the for loop is implemented in ASM, aren't you ? So the implementation of the for loop is not good and also a lot of other compilers are concerned, ouch !. The behaviour is not as expected in the C routine. Do you agree ? Regards, Ramucho BTW, I made a little mistake in my code emulation for the for loop, here is the correct code : unsigned char x = 0, y = 0; do { // My code (display x on LCD) y = (x != 255); x += y; } while(y); // instead of while(!y) -------------- Ouch ! That's really the way a for loop runs in that special case It's not the thing I expected to. So, ok, I change the way I program this routine (char x -> int x, for example). I'll take care of this problem for future. Thanks for all Regards, Ramucho
2. ## "for" Loop Infinite

I think for almost everybody else the code does exactly what they would expect.x is of type char (8 bit data), therefore its value can never exceed 255, therefore the loop never terminates. Regards Dave Yes, for 99.9% it's ok if you don't have to use the extreme values of the variable used in a for loop (0 and 255) . As you're saying, an 8 bit variable can take 256 values from 0 to 255, I know that. So, why do you speak of values over 255 ? There's nothing like that written in the C code. You're probably speaking of how the for loop is implemented in ASM, aren't you ? So the implementation of the for loop is not good and also a lot of other compilers are concerned, ouch !. The behaviour is not as expected in the C routine. Do you agree ? Regards, Ramucho BTW, I made a little mistake in my code emulation for the for loop, here is the correct code : unsigned char x = 0, y = 0; do { // My code (display x on LCD) y = (x != 255); x += y; } while(y); // instead of while(!y)
3. ## "for" Loop Infinite

Sorry but I still find it impossible for this code to break out of the loop regardless of what compiler is used. I just tested it under Microsoft Visual Studio and it behaves exactly like BoostC i.e. loop never exits. There must be something else in such code what you maybe haven't noticed. Regards, Pavel I tried this code with Borland C++ Builder and I've the same problem (no loop exit). So, I don't know why it runs well with Hi-Tech compiler (I should take a look at the asm code generated). But it doesn't matter now that I know that it doesn't work. But, for me, it's not logical that such a loop doesn't work. The C code is correct, there is no error and any programmer, in this case, expects that the result is ok. A compiler should be able to generate a correct code. Thanks for your help. Regards, Ramucho
4. ## "for" Loop Infinite

255 is the max value for unsigned char. If you wrote 'x < 255' than loop would exit. But because you wrote 'x <= 255' loop will never exit as unsigned char can never get bigger than 255. So I see no errors in the code generated. I doubt this code did ever exit when compiled by other compiler (Hi-Tech, MikroC) unless they use more than 8 bit long data types for char. Regards, Pavel If I write "x<255", the loop exits, but x = 254 not 255. I'm loosing a value . I can confirm you that when this code is compiled with other compilers it runs ok. Maybe, they're using a tip or a trick in the code they generate, I don't know. So, I need to declare x as int or write my own routine to emulate a for loop. Here what I coded to emulate the for loop : char x = 0, y = 0; do { // My code (display x on LCD) y = (x != 255); x += y; } while(!y); Regards, Ramucho
5. ## "for" Loop Infinite

Nothing wrong with the code if what you wanted to do is an infinite loop. If you expect loop to exit that you need to change your code (Hint: take a look at your counter data type. What makes you think that unsigned char can ever get bigger than 255?) Regards, Pavel Yes, the code is correct but the behaviour is not. I compiled this code with other C compilers (Hi-Tech, MikroC...) and it runs without any problem. I mean that when the variable reaches its max value, the loop ends and the program exits. If I wanted an infinite loop, I would have chosen a while(1){} loop. That's why I insist on the fact that the generated code for this function is not 100% correct. 255 is the max value for an unsigned char isn't it ? So why can't I reach this value without changing the way I coded it ? Kind regards, Ramucho
6. ## "for" Loop Infinite

Hello, I'm using BoostC compiler v6.70 with Flowcode. I have a problem with the following code : char x; for(x = 0; x <= 255; x++) { // my code here (display x on a LCD) } Well, this code is correct but doesn't run correctly. It's an infinite loop !? What's wrong with it ? If I declare int x instead of char x, it is ok. I think it's a problem of the generated asm code for this program ("for" loop). Anybody has an idea ? Algorithm for the "for loop" should be something like this : 1 - Loop variable initialisation 2 - start : // my code here 3 - if(variable < max value) { variable incrementation } else go to end 4 - go to start 5 - end :
×