Topic: i = 1; i < 5 or i <=4;i++


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/07/18
Raw View
Antoine Leca writes:

>> >       for (int i = i; i < 5; i++)

> However, I don't understand how the second form can have
> defined behavior, and why i would be initialized to 1.

> I hope the future wording of C9X will:

> - begin the visibility of i after the ";", thus forbidding
> i to be used in the initializer
> - and hence forbid such a construction to exist.

What about:

           for (int i = foo(), j = i; i < 5; i++)

Couldn't i be used ot initialize j?  The wording you suggest prevents
this.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Antoine Leca <Antoine.Leca@renault.fr>
Date: 1997/07/03
Raw View
[I added comp.std.c because I understand C9X will have this
 feature, and comp.std.c++ for having C++ experts' point of view]

Steve Summit wrote in <5p7b06$35f$1@eskinews.eskimo.com>:
>
> In article <33B66443.55B77ED7@netcom.ca>, "Adam S. Nickerson"
> <asn@netcom.ca> writes:
...
> >       for (int i = i; i < 5; i++)
>
> I consider this marginally poor style.  Four trips through the
> loop will be taken (with i equal to 1, 2, 3, and 4), but a
> careless glance at the "5" up there might suggest five loops.
> (Also, that "int" in there is valid in C++, but not in C.)

I agree with Steve's comments, of course.

However, I don't understand how the second form can have
defined behavior, and why i would be initialized to 1.

I hope the future wording of C9X will:
- begin the scope of the i object at the begining of the
for statement (thus hiding any surrounding i object)
- begin the visibility of i after the ";", thus forbidding
i to be used in the initializer
- and hence forbid such a construction to exist.


Antoine
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Clive D.W. Feather" <clive@on-the-train.demon.co.uk>
Date: 1997/07/04
Raw View
In article <33BB9640.5E7E@Renault.FR>, Antoine Leca
<Antoine.Leca@renault.fr> writes
>>>       for (int i = i; i < 5; i++)
>>
>> I consider this marginally poor style.  Four trips through the
>> loop will be taken (with i equal to 1, 2, 3, and 4), but a
>> careless glance at the "5" up there might suggest five loops.

>However, I don't understand how the second form can have
>defined behavior, and why i would be initialized to 1.

I had presumed it was a typo for "1".

>I hope the future wording of C9X will:
>- begin the scope of the i object at the begining of the
>for statement (thus hiding any surrounding i object)
>- begin the visibility of i after the ";", thus forbidding
>i to be used in the initializer
>- and hence forbid such a construction to exist.

No, the wording treats it just like:

    {
        int i = i;
        for (; i < 5; i++)
            // body
    }

--
Clive D.W. Feather    | Director of Software Development  | Home email:
Tel: +44 181 371 1138 | Demon Internet Ltd.               | <clive@davros.org>
Fax: +44 181 371 1037 | <clive@demon.net>                 |
Written on my laptop; please observe the Reply-To address |
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]