home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16205 < prev    next >
Encoding:
Text File  |  1992-11-13  |  2.2 KB  |  65 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.centerline.com!noc.near.net!news.Brown.EDU!qt.cs.utexas.edu!cs.utexas.edu!uwm.edu!linac!att!cbnewsc!cbfsb!cbnewsg.cb.att.com!nh
  3. From: nh@cbnewsg.cb.att.com (nicholas.hounsome)
  4. Subject: Re: Bad 'for' statement?
  5. Message-ID: <1992Nov13.085913.1912@cbfsb.cb.att.com>
  6. Sender: news@cbfsb.cb.att.com
  7. Organization: AT&T
  8. References: <1992Nov12.160420.18097@rs6000.bham.ac.uk>
  9. Date: Fri, 13 Nov 1992 08:59:13 GMT
  10. Lines: 53
  11.  
  12. From article <1992Nov12.160420.18097@rs6000.bham.ac.uk>, by pickerig@eee.bham.ac.uk (Mr. G. Pickering):
  13. > Hi,
  14. > Could you tell me if this is really a syntax error as AT&T 2.1 reports?
  15.  
  16. Yes it is.
  17.  
  18. >   for(int i=0, int j=10;   i<10;   i++, j++)
  19. >   {
  20. >     // do something
  21. >   }
  22.  
  23. The first part must be either an expression or a declaration statement.
  24.  
  25. The usual C
  26.  
  27. for(i=0,j=10 ...
  28.  
  29. is an expression in which ',' is an operator NOT a separator - this tends to be
  30. overlooked because ',' is rarely used as an operator.
  31. If you wrote:
  32.  
  33. int i=0, int j = 10;
  34.  
  35. You would not expect it to compile and so it wont in the "for".
  36. Of course in this case it is possible to write:
  37.  
  38. for(int i=0,j=10;...
  39.  
  40. Since the first part is a valid declaration statement. One  can even have ints
  41. , arrays of ints, functions returning int or any combination of these but not
  42. an int and a short say. It is only included as a convenience for a 
  43. common programming construct and IMHO it was a mistake. (It would have
  44. been better if the scope was limmited to the for loop - what I find is 
  45. that I want several fors and I cannot use the same variable in each.
  46.  
  47. > Thanks,
  48. > Guy
  49. > --
  50. > +--------------------------------+-----------------------------------------+
  51. > | Guy Pickering                  | School of Electronic & Electrical Eng,  |
  52. > | E-MAIL: G.PICKERING@uk.ac.bham | University of Birmingham,               |
  53. > | Tel: (021) 414 4340            | Edgbaston, Birmingham. B15 2TT.         |
  54. > +--------------------------------+-----------------------------------------+
  55. > | "I have ten pairs of trainers...One for each day of the week" - Sam Fox  |
  56. > +--------------------------------+-----------------------------------------+
  57.  
  58. Nick Hounsome
  59. AT&T Network Systems UK Ltd.
  60.