Discussion:
The while statement with the init-statement
(too old to reply)
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-11-09 14:29:33 UTC
Permalink
What is the reason that the while statement does not have the
init-statement as the if statement has?

For example

while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-11-09 15:35:30 UTC
Permalink
Because we already have it :)

for(/*init*/; /*cond*/;);

On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-11-09 16:26:25 UTC
Permalink
I can't agree with such an approach. If to follow this logic then the while
statement could be removed from the C++ entirely.

As for me I would prefer to use the while statement instead of the for
statement in cases when the *expression *is empty in the corresponding for
statement. In such situations the while statement is more expressive.

пятМОца, 9 МПября 2018 г., 18:35:43 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Because we already have it :)
for(/*init*/; /*cond*/;);
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-11-09 16:39:08 UTC
Permalink
Well, not directly, because writing for (; cond;) would be pretty weird.
But also, when do you need to write a while loop with an init condition
without an increment?

On Fri, Nov 9, 2018 at 8:26 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I can't agree with such an approach. If to follow this logic then the
while statement could be removed from the C++ entirely.
As for me I would prefer to use the while statement instead of the for
statement in cases when the *expression *is empty in the corresponding
for statement. In such situations the while statement is more expressive.
пятМОца, 9 МПября 2018 г., 18:35:43 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Because we already have it :)
for(/*init*/; /*cond*/;);
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Greg Marr
2018-11-11 21:36:55 UTC
Permalink
when do you need to write a while loop with an init condition without an
increment?
One example is looping over a map and erasing some elements. Sometimes you
call erase, which returns the new iterator, and sometimes you increment the
iterator.

for (auto i = mapStuff.begin(); i != mapStuff.end(); ) {
if (some_condition(i)) {
i = mapStuff.erase(i);
} else {
i++;
}
}
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-11-12 19:55:31 UTC
Permalink
A simple example is a while loop like the following while loop that
introduces a local variable

while ( std::string s; std::getline( std::cin, s ) and not std::empty( s ) )
{

// std::cout << s << '\n';
// ...
}



Or another context when a recursive program is rewritten as an iterative
program.
Usually a recursive program has a construction like this

if ( init-statement; condition )
{
// do something
call_itself();
}


That is the if statement looks like a while statement with one iteration.
So to rewrite this part of a recursive function it will be enough to
substitute the if statement for the while statement.

while ( init-statement; condition )
{
// do something
}




пятМОца, 9 МПября 2018 г., 20:39:20 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Well, not directly, because writing for (; cond;) would be pretty weird.
But also, when do you need to write a while loop with an init condition
without an increment?
On Fri, Nov 9, 2018 at 8:26 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I can't agree with such an approach. If to follow this logic then the
while statement could be removed from the C++ entirely.
As for me I would prefer to use the while statement instead of the for
statement in cases when the *expression *is empty in the corresponding
for statement. In such situations the while statement is more expressive.
пятМОца, 9 МПября 2018 г., 18:35:43 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Because we already have it :)
for(/*init*/; /*cond*/;);
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Patrice Roy
2018-11-13 02:05:42 UTC
Permalink
You're so close (indeed, a single ';' character away!) with a for... That's
the reasoning behind the current state of things. I can see virtue in
homogeneity in adding while-init, but given the current, "there are so many
papers to process in so little time" situation, it would probably be useful
to come up with something stronger than "it avoids a ';'". The idea has
merit, but will have a hard time as-is making it to the top of the pile...

Le lun. 12 nov. 2018 à 14:55, 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
A simple example is a while loop like the following while loop that
introduces a local variable
while ( std::string s; std::getline( std::cin, s ) and not std::empty( s ) )
{
// std::cout << s << '\n';
// ...
}
Or another context when a recursive program is rewritten as an iterative
program.
Usually a recursive program has a construction like this
if ( init-statement; condition )
{
// do something
call_itself();
}
That is the if statement looks like a while statement with one iteration.
So to rewrite this part of a recursive function it will be enough to
substitute the if statement for the while statement.
while ( init-statement; condition )
{
// do something
}
пятМОца, 9 МПября 2018 г., 20:39:20 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Well, not directly, because writing for (; cond;) would be pretty weird.
But also, when do you need to write a while loop with an init condition
without an increment?
On Fri, Nov 9, 2018 at 8:26 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I can't agree with such an approach. If to follow this logic then the
while statement could be removed from the C++ entirely.
As for me I would prefer to use the while statement instead of the for
statement in cases when the *expression *is empty in the corresponding
for statement. In such situations the while statement is more expressive.
пятМОца, 9 МПября 2018 г., 18:35:43 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Because we already have it :)
for(/*init*/; /*cond*/;);
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Jordi Vilar
2018-11-13 15:44:33 UTC
Permalink
Sincerely, I only see a real case that may make the difference, and is if
the initialization is executed at each iteration, so the scope is each
single iteration. In this case, we have a legitimate case that cannot be
covered by 'for' alone, and actually matches 'if' converted into a loop.

Consider this example:

```
while(auto *t = get_next_pointer())
{
...
}
```

or this other example:

```
while(auto x = get_some_x_at_each_iteration(); x < upper_limit_for_x)
{
...
}
```

that is, the syntax for 'while' would be exactly the same than for 'if'
(without the else part, of course!). As already stated, the difference with
'for' would be double: the initialization is executed at each iteration AND
the scope for the variable would be limited to the iteration, not the whole
loop.

Whether we really need this new construct or not, it's arguable. For sure
it can be implemented without it:
for(;;)
{
auto *t = get_next_pointer();
if (!t)
break;
....
}

but it is clearer with the new 'while'.
Post by Patrice Roy
You're so close (indeed, a single ';' character away!) with a for...
That's the reasoning behind the current state of things. I can see virtue
in homogeneity in adding while-init, but given the current, "there are so
many papers to process in so little time" situation, it would probably be
useful to come up with something stronger than "it avoids a ';'". The idea
has merit, but will have a hard time as-is making it to the top of the
pile...
Le lun. 12 nov. 2018 à 14:55, 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
A simple example is a while loop like the following while loop that
introduces a local variable
while ( std::string s; std::getline( std::cin, s ) and not std::empty( s ) )
{
// std::cout << s << '\n';
// ...
}
Or another context when a recursive program is rewritten as an iterative
program.
Usually a recursive program has a construction like this
if ( init-statement; condition )
{
// do something
call_itself();
}
That is the if statement looks like a while statement with one iteration.
So to rewrite this part of a recursive function it will be enough to
substitute the if statement for the while statement.
while ( init-statement; condition )
{
// do something
}
пятМОца, 9 МПября 2018 г., 20:39:20 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Well, not directly, because writing for (; cond;) would be pretty weird.
But also, when do you need to write a while loop with an init condition
without an increment?
On Fri, Nov 9, 2018 at 8:26 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
I can't agree with such an approach. If to follow this logic then the
while statement could be removed from the C++ entirely.
As for me I would prefer to use the while statement instead of the for
statement in cases when the *expression *is empty in the corresponding
for statement. In such situations the while statement is more expressive.
пятМОца, 9 МПября 2018 г., 18:35:43 UTC+3 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
Because we already have it :)
for(/*init*/; /*cond*/;);
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it,
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
tkoeppe via ISO C++ Standard - Discussion
2018-11-14 22:02:38 UTC
Permalink
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
As was already pointed out, a "while statement with initializer" already
exists, under the name "for". If you will, it is the original "for"
statement that motivated the addition of init-statements to "if", "switch"
and range-"for".

I realize that there's an extra semicolon at the end that you need to say
even if you have no increment, but I don't think that's important. The
point is that the new facilities for "if", "switch" and range-"for" add
expressiveness to the language by enabling something that was previously
cumbersome and tedious. Previously, people would be likely to *not* write
extra scopes to minimize the scope of local variables, because it would
just look very unsightly. The new syntax offers a natural place for this in
many common use cases.

Nothing analogous is true for "while" statements. If you want to minimize
scopes of local variables, just use a "for" statement instead and you're
done. This is a solved problem.
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
Sincerely, I only see a real case that may make the difference, and is if
the initialization is executed at each iteration, so the scope is each
single iteration. In this case, we have a legitimate case that cannot be
covered by 'for' alone, and actually matches 'if' converted into a loop.
*while (auto *t = get_next_pointer())*
*{*
* // ...*
*}*
I wasn't entirely sure what you were saying here, so just to be clear, in
case there was some uncertainty: the above syntax already exists, with the
intended meaning, and has indeed always been valid in C++. (The *condition*
grammar production is used by "for", "while" and "if" and includes the case
of declarations of things that are contextually convertible to bool.) You
can, and have always been able to, say things like "*for (char buf[10]; int
n = GetBytes(buf, 10);) { SendBytes(buf, buf + n); }*", which breaks once
GetBytes returns 0. (I'm sorry if I misunderstood you and you were actually
already saying that!)
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-11-16 13:46:15 UTC
Permalink
You may use the for statement instead of the while statement everywhere.
But I'm sure other programmers prefer to use a better style of programming.
Post by Nicolas Lesser
On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
As was already pointed out, a "while statement with initializer" already
exists, under the name "for". If you will, it is the original "for"
statement that motivated the addition of init-statements to "if", "switch"
and range-"for".
I realize that there's an extra semicolon at the end that you need to say
even if you have no increment, but I don't think that's important. The
point is that the new facilities for "if", "switch" and range-"for" add
expressiveness to the language by enabling something that was previously
cumbersome and tedious. Previously, people would be likely to *not* write
extra scopes to minimize the scope of local variables, because it would
just look very unsightly. The new syntax offers a natural place for this in
many common use cases.
Nothing analogous is true for "while" statements. If you want to minimize
scopes of local variables, just use a "for" statement instead and you're
done. This is a solved problem.
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
Sincerely, I only see a real case that may make the difference, and is if
the initialization is executed at each iteration, so the scope is each
single iteration. In this case, we have a legitimate case that cannot be
covered by 'for' alone, and actually matches 'if' converted into a loop.
*while (auto *t = get_next_pointer())*
*{*
* // ...*
*}*
I wasn't entirely sure what you were saying here, so just to be clear, in
case there was some uncertainty: the above syntax already exists, with the
intended meaning, and has indeed always been valid in C++. (The
*condition* grammar production is used by "for", "while" and "if" and
includes the case of declarations of things that are contextually
convertible to bool.) You can, and have always been able to, say things
like "*for (char buf[10]; int n = GetBytes(buf, 10);) { SendBytes(buf,
buf + n); }*", which breaks once GetBytes returns 0. (I'm sorry if I
misunderstood you and you were actually already saying that!)
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Bryce Adelstein Lelbach aka wash
2018-11-12 04:25:39 UTC
Permalink
What led you to want this? What is the use case?

On Fri, Nov 9, 2018, 6:29 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
What is the reason that the while statement does not have the
init-statement as the if statement has?
For example
while ( init-statementopt condition ) statement
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...