Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
dolik.rce is currently offline  dolik.rce
Messages: 1779
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Smile ).

Bye, Honza
Re: Multiple statements in for loop [message #18437 is a reply to message #18436] Tue, 30 September 2008 20:44 Go to previous messageGo to next message
bytefield is currently offline  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 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 815
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor

As long as the two variables have same type, the:
bool a = true, b = true;

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. Wink

[Updated on: Tue, 30 September 2008 20:47]

Report message to a moderator

Re: Multiple statements in for loop [message #18439 is a reply to message #18436] Tue, 30 September 2008 21:01 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1779
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

I'm ashamed that I missed so obvious solution Embarassed Probably because I ussualy declare only 1 variable per line if I initialize them.

What if we extend this problem a bit? Twisted Evil
Let's say, one would like to have there two variables of different type. Then it's impossible to write
for(bool b=true,int i=0;...;...){}
I tried this, but it seems that anything after "bool" is handled as declaration of bool.

Quote:

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.


Yes, I'm aware of that Smile I was just playing with this idea before I found the right way to do it using just one variable.

Anyway, thanks for quick response Smile

Edit: Oups I've missed that mr_ped already thought about the two types case... Embarassed sorry

[Updated on: Tue, 30 September 2008 21:02]

Report message to a moderator

Previous Topic: explanation of c++ typedef line
Next Topic: I don't get some aspects of STL ... [pointless rant]
Goto Forum:
  


Current Time: Sat Jan 23 09:39:31 CET 2021

Total time taken to generate the page: 0.00911 seconds