for loop compile error

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 2
Reputation: EBaller55 is an unknown quantity at this point 
Solved Threads: 0
EBaller55 EBaller55 is offline Offline
Newbie Poster

for loop compile error

 
0
  #1
Oct 3rd, 2008
I am using Visual C++ 2005 with SP1

why does the following code generate a compiler error (error C2062: type 'int' unexpected)?

  1. for(int i=0,int j=0;i<5;i++,j++){}

Note the following two variations generate no compile error

  1. int i,j;
  2. for(i=0,j=0;i<5;i++,j++){}

  1. int j;
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: for loop compile error

 
0
  #2
Oct 3rd, 2008
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
  1. int a, int b; // error
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: for loop compile error

 
0
  #3
Oct 3rd, 2008
It should be:

  1. for(int i=0, j=0;i<5;i++,j++){}
Last edited by Denniz; Oct 3rd, 2008 at 2:49 am. Reason: Typo
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: EBaller55 is an unknown quantity at this point 
Solved Threads: 0
EBaller55 EBaller55 is offline Offline
Newbie Poster

Re: for loop compile error

 
0
  #4
Oct 3rd, 2008
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:

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: for loop compile error

 
0
  #5
Oct 3rd, 2008
The for statement syntax:
  1. for (for-init-statement condition(opt); expression)
  2. for-init-statement::
  3. expression-statement
  4. simple-declaration
  5. expression-statement::
  6. expression(opt) ;
Informally simple-declaration is well-known type names-with-possible-initializators construct. The semicolon token terminates construct in C++.

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).
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: for loop compile error

 
0
  #6
Oct 3rd, 2008
Using nested loops would be one solution
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: for loop compile error

 
0
  #7
Oct 4th, 2008
Originally Posted by EBaller55 View Post
It would be nice because then I could do the following:

  1. 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.
I disagree. That to me is confusing and unclean.

On the other hand:
  1. for (int i = 0; i < 5; i++)
  2. {
  3. double d = 1;
  4. cout << "1.5^" << i << "=" << d << endl;
  5. d *= 1.5;
  6. }
This is much easier to read because
1) i is the only variable important to the for statement itself
2) 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: for loop compile error

 
0
  #8
Oct 4th, 2008
Regrettably, it does not work (d is local auto var in the loop body block):
  1. for (int i = 0; i < 5; i++)
  2. {
  3. double d = 1; // new incarnations for 0, 1, 2, 3, 4...
  4. cout << "1.5^" << i << "=" << d << endl;
  5. d *= 1.5;
  6. }
Right solution:
  1. double d = 1.0;
  2. for (int i = 0; i < 5; ++i, d*=1.5)
  3. {
  4. cout << "1.5^" << i << "=" << d << endl;
  5. }
Last edited by ArkM; Oct 4th, 2008 at 4:47 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 704 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC