-4

Is it legal to have the following for loops in c and c++:

for(double d=2.0; d<3.1; d+=0.1)
6
  • 7
    Legal as in compiles? Or as in "is it a good idea"? because the first you can easily check.
    – Borgleader
    Sep 18, 2013 at 14:10
  • I just want to know whether it is legal for loop syntax or not Sep 18, 2013 at 14:11
  • 1
    @user2131316 It's legal, but the inaccuracies in floating point arithmetic means it's bad idea unless you know what you are doing.
    – john
    Sep 18, 2013 at 14:12
  • 1
    Why not run a compiler and see if it throws an error?
    – junix
    Sep 18, 2013 at 14:12
  • 4

8 Answers 8

8

It is legal syntax for C99 and C++. And it is deterministic (should end) as long as d isn't improperly fiddled with inside the loop.

Although the loop will indeed terminate, it's unclear exactly how many loop iterations will occur. On the 12th iteration, the value of d will be incremented to, theoretically, 3.1, but it may be 3.099999.. (or something similar) or it could be 3.1000...01 (or something similar) due to rounding. So whether the loop will do 11 or 12 iterations is unclear. You don't want to rely on exact matches in floating point.

2
  • It should be deterministic for standard floating point calculations yes, but can you say without testing, what is going to be value of d in final iteration rounded to 1 decimal place?
    – hyde
    Sep 18, 2013 at 14:42
  • @hyde, indeed I meant in terms of the loop should end, as I indicated parenthetically in my comment. But as has been pointed out in other comments, the exact number of iterations isn't clear due to rounding.
    – lurker
    Sep 18, 2013 at 14:46
3

you also can have the following

for(char c='a'; c<'A'; c+=1)
 printf("%c", c);
4
  • 3
    You can also have for (static struct { int i; double d; } x {0,0}; x.i < ++x.d; ++x.i) ;...
    – jrok
    Sep 18, 2013 at 14:16
  • is it looping until the d overflow?
    – aah134
    Sep 18, 2013 at 14:22
  • It'll probably loop forever, don't know :)
    – jrok
    Sep 18, 2013 at 14:31
  • yes, turned out that double don't overflow :D
    – aah134
    Sep 18, 2013 at 14:39
1

Yes, it is, there is no restriction about it. In C++ is also very common creating for loops with iterators.

1

This is legal in C and C++ but might not have the behavior you expect unless you understand floating-point and use it properly.

Rounding will likely cause the value of d to be slightly different from the desired endpoint, so the loop might perform one iteration more or less than you desired, depending on circumstances. (In particular, neither 3.1 nor .1 are exactly representable in the floating-point format most commonly in use. The source texts 3.1 and .1 are converted to approximations.)

There are usually better ways to write loops that use a sequence of floating-point values, such as using an integer for loop control and calculating the floating-point value from the integer in each iteration. (Alternatively, you can use a floating-point object for loop control but take care to use only integer values for the loop expressions.)

1

As far as the languages are concerned, there is no "index variable" in for loop. There are just 3 expressions which can be whatever you want as long as they are legal expressions.

You could also say, index variable of for loop is just a convention, human concept. So you need to ask, can index variable (however you choose to define that term) be of type double? If you are using it as C array index, then I'd say "no", even though implicit cast makes such code compile.

However, it is a good idea to use for loop according to certain conventions. Your question code is in my opinion fine use of for loop, except end condition should be something like d < (3.1 - 0.00001) to allow for rounding errors. Exact value and direction of this adjustment depend on loop details, programmer needs to be very careful to get it right (and test it too).

Some other code using double variable might be better to write using while loop, such as if increment/decrement is more complex or conditional. Note that same rounding error concerns apply then,too.

0

in c++ (Microsoft VC++), this is fine.

however, in c, i think you need to declare the variable d beforehand, i.e. at the begin of the function.

1
  • This silly limitation was abandoned in C99. :)
    – user1804599
    Sep 18, 2013 at 14:20
0

Seems to be legal in c!! You can also declare of the variable d outside the loop.

0
for(double d=2.0; d<3.1; d+=0.1)

is a valid syntax in C99 and C++ but scope of the identifier d is limited to this loop. Also once the loop is executed, d no longer exixts.

2
  • 1
    d can be accessed outside the loop by passing its address to a function. You mean the scope of the identifier d is limited to this loop and the lifetime of the object it names is limited to the time during which this loop is executed. Sep 18, 2013 at 14:18
  • @EricPostpischil; Yes I mean that.
    – haccks
    Sep 18, 2013 at 14:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.