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 previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
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
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: explanation of c++ typedef line
Next Topic: I don't get some aspects of STL ... [pointless rant]
Goto Forum:
  


Current Time: Fri Apr 19 11:40:06 CEST 2024

Total time taken to generate the page: 0.27434 seconds