Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The for-init-statement in a for statement

13 views
Skip to first unread message

Matthias Hofmann

unread,
Jul 22, 2006, 2:24:13 PM7/22/06
to
Hello everybody!

When I first learned learned C++, I often encountered statements like

for ( ;; ) {}

instead of

while ( true ) {}

Also, I found code in the VC++ implementation of STL algorithms that omitted
the for-init-statement in for statements, for example in:

template <class _FwdIt, class _Ty> inline
void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
{
for( ; _First != _Last; ++_First )
_First = _Val;
}

This seems to make perfect sense, but 6.5.3/1 of the C++ Standard says that
the for-init-statement cannot be omitted. Does that mean that the
implementation of fill() above is not portable? Would it have to be
rewritten as follows to be standard compliant?

template <class _FwdIt, class _Ty> inline
void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
{
while ( _First != _Last )
{
_First = _Val;
++_First;
}
}

By the way, I noticed that my version of the C++ Standard defines the for
statement as follows:

for ( for-init-statement condition opt ; expression opt ) statement

I guess there is a semicolon missing right before "condition"?

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

James Kanze

unread,
Jul 23, 2006, 8:16:08 AM7/23/06
to
Matthias Hofmann wrote:

> When I first learned learned C++, I often encountered statements like

> for ( ;; ) {}

> instead of

> while ( true ) {}

> Also, I found code in the VC++ implementation of STL
> algorithms that omitted the for-init-statement in for
> statements, for example in:

> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> for( ; _First != _Last; ++_First )
> _First = _Val;
> }

> This seems to make perfect sense, but 6.5.3/1 of the C++
> Standard says that the for-init-statement cannot be omitted.

It most certainly can't. And isn't, in either of the above
cases.

The for-init-statement can be an expression statement, however,
and the expression in an expression statement is optional.

[...]


> By the way, I noticed that my version of the C++ Standard
> defines the for statement as follows:

> for ( for-init-statement condition opt ; expression opt ) statement

> I guess there is a semicolon missing right before "condition"?

Not at all. A for-init-statement can be either a declaration or
an expression statement, and both of these end with a semicolon.

The original C did not allow a declaration here, and the grammar
was:

for ( expression[opt] ; expression[opt] ; expression[opt] ) statement

When the language was changed to allow a declaration (which is a
statement) as the first term, the grammar was changed to what
you now see (except that conditional didn't replace the second
expression until much later). Since the statement contains a
semi-colon, it cannot be part of the grammar of the for
statement (or you would need two semi-colons).

--
James Kanze kanze...@neuf.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

Heinz Ozwirk

unread,
Jul 23, 2006, 8:15:22 AM7/23/06
to
"Matthias Hofmann" <hof...@anvil-soft.com> schrieb im Newsbeitrag
news:44c2219a$0$24892$9b4e...@newsread4.arcor-online.net...

No. It is not missing there. Look at the definition of for-init-statement
and follow the "links" you find there. You'll find out that a
for-init-statement may be an "expression opt ;" or a "decl-specifier-seq opt
init-declartor-list opt ;" Both of them bring their own semicolon and so it
is not missing in the definition of a for statement. If the semicolon, which
you think is missing, were present, you would have to write something like
"for (int i = 0;; i < 10; ++i) ...", which is probably not what anyone would
like to write.

But there seems to be a bug in the standard (at least the PDF file I have).
The for-init-statement in a for statement like "for (; ...)" might be an
expression-statement with an empty expression or a simple-declaration where
decl-specifier-seq and init-declarator-list are both empty.

Heinz

Bo Persson

unread,
Jul 23, 2006, 8:20:07 AM7/23/06
to

"Matthias Hofmann" <hof...@anvil-soft.com> skrev i meddelandet
news:44c2219a$0$24892$9b4e...@newsread4.arcor-online.net...

> Hello everybody!
>
> When I first learned learned C++, I often encountered statements
> like
>
> for ( ;; ) {}
>
> instead of
>
> while ( true ) {}
>
> Also, I found code in the VC++ implementation of STL algorithms that
> omitted
> the for-init-statement in for statements, for example in:
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> for( ; _First != _Last; ++_First )
> _First = _Val;
> }
>
> This seems to make perfect sense, but 6.5.3/1 of the C++ Standard
> says that
> the for-init-statement cannot be omitted. Does that mean that the
> implementation of fill() above is not portable? Would it have to be
> rewritten as follows to be standard compliant?

No, the for-init-statment has one alternative where the expression is
optional, and only the semicolon is required. That is why there is a
semicolon directly inside the opening parenthesis.

>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> while ( _First != _Last )
> {
> _First = _Val;
> ++_First;
> }
> }
>
> By the way, I noticed that my version of the C++ Standard defines
> the for
> statement as follows:
>
> for ( for-init-statement condition opt ; expression opt ) statement
>
> I guess there is a semicolon missing right before "condition"?


No, the for-init-statement is defined as

expression(opt) ;

so it contains the semicolon (but perhaps not an expression).


Bo Persson

Jiang

unread,
Jul 23, 2006, 8:38:38 AM7/23/06
to
Matthias Hofmann wrote:
> Hello everybody!
>
> When I first learned learned C++, I often encountered statements like
>
> for ( ;; ) {}
>
> instead of
>
> while ( true ) {}
>
> Also, I found code in the VC++ implementation of STL algorithms that omitted
> the for-init-statement in for statements, for example in:
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> for( ; _First != _Last; ++_First )
> _First = _Val;
> }
>
> This seems to make perfect sense, but 6.5.3/1 of the C++ Standard says that
> the for-init-statement cannot be omitted. Does that mean that the
> implementation of fill() above is not portable? Would it have to be
> rewritten as follows to be standard compliant?
>


But nothing was omitted here.

Please note expression

;

is a perfect valid expression.


> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> while ( _First != _Last )
> {
> _First = _Val;
> ++_First;
> }
> }
>
> By the way, I noticed that my version of the C++ Standard defines the for
> statement as follows:
>
> for ( for-init-statement condition opt ; expression opt ) statement
>
> I guess there is a semicolon missing right before "condition"?
>

No. The standard say it explicitly that "a for-init-statement ends
with a semicolon".

Jack Klein

unread,
Jul 23, 2006, 8:36:02 AM7/23/06
to
On 22 Jul 2006 14:24:13 -0400, "Matthias Hofmann"
<hof...@anvil-soft.com> wrote in comp.lang.c++.moderated:

> Hello everybody!
>
> When I first learned learned C++, I often encountered statements like
>
> for ( ;; ) {}
>
> instead of
>
> while ( true ) {}
>
> Also, I found code in the VC++ implementation of STL algorithms that omitted
> the for-init-statement in for statements, for example in:
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> for( ; _First != _Last; ++_First )
> _First = _Val;
> }
>
> This seems to make perfect sense, but 6.5.3/1 of the C++ Standard says that
> the for-init-statement cannot be omitted. Does that mean that the
> implementation of fill() above is not portable? Would it have to be
> rewritten as follows to be standard compliant?
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> while ( _First != _Last )
> {
> _First = _Val;
> ++_First;
> }
> }
>
> By the way, I noticed that my version of the C++ Standard defines the for
> statement as follows:
>
> for ( for-init-statement condition opt ; expression opt ) statement
>
> I guess there is a semicolon missing right before "condition"?

I think that the grammar shown in 6.5.3 para1 has a typographical
error, in both the 1998 and 2003 versions of the document.

The incorrect grammar with the same error is also shown in 6.5 para1
in both 1998 and 2003 versions.

Despite the typographical errors, omitting the for-init-statement is
quite valid in C++, just as it has always been in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Kai-Uwe Bux

unread,
Jul 23, 2006, 8:34:33 AM7/23/06
to
Matthias Hofmann wrote:

> Hello everybody!
>
> When I first learned learned C++, I often encountered statements like
>
> for ( ;; ) {}
>
> instead of
>
> while ( true ) {}
>
> Also, I found code in the VC++ implementation of STL algorithms that
> omitted the for-init-statement in for statements, for example in:
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> for( ; _First != _Last; ++_First )
> _First = _Val;
> }
>
> This seems to make perfect sense, but 6.5.3/1 of the C++ Standard says
> that the for-init-statement cannot be omitted. Does that mean that the
> implementation of fill() above is not portable?

No it does not mean that. The code is fine. (Explanation below)

> Would it have to be rewritten as follows to be standard compliant?
>
> template <class _FwdIt, class _Ty> inline
> void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
> {
> while ( _First != _Last )
> {
> _First = _Val;
> ++_First;
> }
> }
>
> By the way, I noticed that my version of the C++ Standard defines the for
> statement as follows:
>
> for ( for-init-statement condition opt ; expression opt ) statement
>
> I guess there is a semicolon missing right before "condition"?

Nope: a for-init-statement is either an expression statement or a simple
declaration. In both cases, the statement ends in a semicolon -- the
semicolon is not a statement separator in the C++ syntax as defined by the
standard but part of the statement (well part of those that require a
semicolon).

This also might explain, why you are not allowed to omit the statement: the
semicolon would be missing.


Finally, note that ";" is an expression statement.

Best

Kai-Uwe Bux

Peter C. Chapin

unread,
Jul 23, 2006, 8:42:37 AM7/23/06
to
Matthias Hofmann wrote:

> By the way, I noticed that my version of the C++ Standard defines the for
> statement as follows:
>
> for ( for-init-statement condition opt ; expression opt ) statement
>
> I guess there is a semicolon missing right before "condition"?

The semicolon is embedded in the syntax for the for-init-statement. In
fact, looking at the grammar summary in the standard I see the following

for-init-statement ::=
expression-statement
| simple-declaration

An expression-statement can just consist of a semicolon (that is, it can
be empty). So it seems to me that using an empty for-init-statement,
aside from the semicolon, is allowed. I don't see any text that
explicitly prohibits it. Am I missing something?

Peter

James Dennett

unread,
Jul 23, 2006, 8:58:40 PM7/23/06
to
Jiang wrote:
> Please note expression
>
> ;
>
> is a perfect valid expression.

It is not a valid expression; it is, however, a valid
expression-statement, as the expression in an
expression-statement is optional.

-- James

Heinz Ozwirk

unread,
Jul 24, 2006, 9:44:08 AM7/24/06
to
"Jiang" <goo.m...@yahoo.com> schrieb im Newsbeitrag
news:1153647178.6...@m79g2000cwm.googlegroups.com...

> Please note expression
>
> ;
>
> is a perfect valid expression.

It's a valid expression-statement, but not an expression.

Heinz

0 new messages