| | |
for loop compile error
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
I am using Visual C++ 2005 with SP1
why does the following code generate a compiler error (error C2062: type 'int' unexpected)?
Note the following two variations generate no compile error
Is this just a bug in the Visual C++ compiler or is this the way the standard is defined?
Thanks.
why does the following code generate a compiler error (error C2062: type 'int' unexpected)?
C++ Syntax (Toggle Plain Text)
for(int i=0,int j=0;i<5;i++,j++){}
Note the following two variations generate no compile error
C++ Syntax (Toggle Plain Text)
int i,j; for(i=0,j=0;i<5;i++,j++){}
C++ Syntax (Toggle Plain Text)
int j; for(int i=0,j=0;i<5;i++,j++){}
Is this just a bug in the Visual C++ compiler or is this the way the standard is defined?
Thanks.
Last edited by EBaller55; Oct 3rd, 2008 at 12:49 am.
You can only specify one type of loop counter.
>>Is this just a bug in the Visual C++ compiler
Not a bug. That type a statement is never allowed
>>Is this just a bug in the Visual C++ compiler
Not a bug. That type a statement is never allowed
C++ Syntax (Toggle Plain Text)
int a, int b; // error int a,b; // ok
Last edited by Ancient Dragon; Oct 3rd, 2008 at 1:03 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Last edited by Denniz; Oct 3rd, 2008 at 2:49 am. Reason: Typo
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
OK thanks. All these years of programming and I never knew i++,j++; was a valid C++ line of code. I would always just write two lines of code. I thought it was just a special case in the header of a for loop and assumed the int i=0, int j=0 would work the same way. Interestingly, a google search for "for(int i=0,int j=0" will give some hits, one of which is from books.google.com Do some compilers support this? It would be nice because then I could do the following:
This would make for cleaner code as it is clear that i and d are used only inside the for loop.
C++ Syntax (Toggle Plain Text)
for(int i=0, double d=1;i<5;i++,d*=1.5){cout<<"1.5^"<<i<<"="<<d<<endl;}
This would make for cleaner code as it is clear that i and d are used only inside the for loop.
Last edited by EBaller55; Oct 3rd, 2008 at 1:36 pm.
The for statement syntax:
Informally simple-declaration is well-known type names-with-possible-initializators construct. The semicolon token terminates construct in C++.
Obviously,
About
C++ Syntax (Toggle Plain Text)
for (for-init-statement condition(opt); expression) for-init-statement:: expression-statement simple-declaration expression-statement:: expression(opt) ;
Obviously,
int i = 0, int j = 0; and especially int i = 0, double d = 1; are not valid C++ declarations.About
i++, j++ - that's another matter. It's an expression with comma operator (evaluate left part and discard its result then evaluate right part - it's an expression result). •
•
•
•
It would be nice because then I could do the following:
C++ Syntax (Toggle Plain Text)
for(int i=0, double d=1;i<5;i++,d*=1.5){cout<<"1.5^"<<i<<"="<<d<<endl;}
This would make for cleaner code as it is clear that i and d are used only inside the for loop.
On the other hand:
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 5; i++) { double d = 1; cout << "1.5^" << i << "=" << d << endl; d *= 1.5; }
1) i is the only variable important to the
for statement itself2) d is defined to be used only in the loop
3) formatting and whitespace makes the code much easier to read
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Regrettably, it does not work (d is local auto var in the loop body block):
Right solution:
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 5; i++) { double d = 1; // new incarnations for 0, 1, 2, 3, 4... cout << "1.5^" << i << "=" << d << endl; d *= 1.5; }
C++ Syntax (Toggle Plain Text)
double d = 1.0; for (int i = 0; i < 5; ++i, d*=1.5) { cout << "1.5^" << i << "=" << d << endl; }
Last edited by ArkM; Oct 4th, 2008 at 4:47 am.
![]() |
Similar Threads
- Loop back to previous loop? (C++)
- undeclared identifier error (C++)
- VB6 - Error: Loop Without a Do (Visual Basic 4 / 5 / 6)
- how do you loop fscanf until EOF in c? (C)
- how do you loop fscanf until EOF in c? (C)
- Simple while loop in c++ (C++)
- Help with compile error (C++)
- obsolete binding at `a' error C++ (C++)
- Code not working for deleting recordfrom a file (Visual Basic 4 / 5 / 6)
- Error Linking KeyLogger.exe (C++)
Other Threads in the C++ Forum
- Previous Thread: [0-9] combination Problem
- Next Thread: pls help!!!!!!! round robin algortihm
Views: 704 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






