Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Multiple statements in for loop
Multiple statements in for loop [message #18436] |
Tue, 30 September 2008 20:31  |
|
Hi everybody!
I've just came to interesting problem while playing with a for loop controlled by two variables. Can somebody please tell me what's wrong with this code:for(bool first=true,bool cond=true;
first==true||cond==true;
cond=!(a>=3),first=false)
{
a++;
} It won't compile (using g++4.1) complaining about "error: expected unqualified-id before ‘bool’" in the first line.
When I declare the second bool before the loop like this:bool cond;
for(bool first=true,cond=true;
first==true||cond==true;
cond=!(a>=3),first=false)
{
a++;
} it compiles without errors...
So, my question is: Is this how the compiler is supposed to work? From what I've read, it should be allowed to use multiple declaration and/or expression statements (to cite concrete reference: http://msdn.microsoft.com/en-us/library/b80153d8.aspx). I'd like to know how to write it correctly, while keeping both variables defined only in the for loop scope (I mean any other way than enclosing it all into another set of {} braces ).
Bye, Honza
|
|
|
Re: Multiple statements in for loop [message #18437 is a reply to message #18436] |
Tue, 30 September 2008 20:44   |
bytefield
Messages: 210 Registered: December 2007
|
Experienced Member |
|
|
Why do you not use something like this?
for(bool first = true, cond = true;
first==true||cond==true;
cond=!(a>=3),first=false)
{
a++;
}
In that way you keep both variables defined in for loop.
cdabbd745f1234c2751ee1f932d1dd75
|
|
|
Re: Multiple statements in for loop [message #18438 is a reply to message #18436] |
Tue, 30 September 2008 20:45   |
mr_ped
Messages: 826 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
As long as the two variables have same type, the:
looks ok to me, the same declaration as outside of for can be used.
(and both are scoped as local variables)
The interesting question is what happens when you have:
bool b = false; //outer scope
for ( bool a, b; ...
Is "b" still new variable?
I was so curious, that I had to try.
Yes, the second "b" is new local variable scoped to "for" only.
(both MSC8 and MINGW)
If you want two different types... how to?? I have no idea, looks impossible to me, to define them inside the "for".
Anyway allocating 2+ variables in for-init can often lead to less readable source, so use with caution. I think it's better to avoid it whenever possible.
Edit: I completely missed the "bool cond;" line from original post, so I was sort of thinking you already have correct solution for this case. Sorry, keep my mistake in mind when reading my reply, otherwise it may not make any sense.
[Updated on: Tue, 30 September 2008 20:47] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Wed Apr 30 01:44:59 CEST 2025
Total time taken to generate the page: 0.01295 seconds
|