home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17013 < prev    next >
Encoding:
Internet Message Format  |  1992-11-22  |  2.8 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!bu.edu!news.tufts.edu!news.tufts.edu!gowen
  2. From: gowen@jade.tufts.edu (G. Lee Owen)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: If statement question
  5. Message-ID: <GOWEN.92Nov22235007@jade.tufts.edu>
  6. Date: 23 Nov 92 04:50:38 GMT
  7. Sender: news@news.tufts.edu (USENET News System)
  8. Distribution: na
  9. Organization: Tufts University - Medford, MA
  10. Lines: 65
  11.  
  12.  
  13.     
  14.     Sorry, took me a day extra to get this out, but here we go...
  15.  
  16. From ckkoo@magnus.acs.ohio-state.edu (Chee K Koo) on 20 Nov 92:
  17. CKK> I am new to C programming, while i was doing a numerical analysis
  18. CKK> program which uses many for loop
  19. CKK> here is the loop looks like
  20. CKK> for(i=1;i<=n;i++);
  21. CKK> {
  22. CKK> }
  23. CKK> i put the ";" after the loop header, when the program is excuted, the
  24. CKK> loop is skipped, however, it works fine when the ";" is removed.
  25.     
  26.   When you say 'for(i=1;i<=n;i++);' you are really telling the
  27. compiler 'for(i=1;i<=n;i++) /* DO NOTHING */;'.  The for loop will
  28. work on the (single) statement following it OR on the block following
  29. it, where { and } mark off the block.  
  30.   If you were writing a loop that would execute one statement, it
  31. would be like this, right?
  32.     for(i=1;i<=n;i++) printf("Hi #%d\n", i);
  33. Notice how there is no ; between the end of the loop parens and the
  34. printf statement.  Well, the same thing goes for a for loop with a
  35. block following it.  Say you had to execute two statements, you would
  36. put them in a block:
  37.     for(i=1;i<=n;i++) 
  38.         {
  39.         printf("Hi ");
  40.         printf("#%d\n", i);
  41.     }
  42. like such. The output of this would be (n == 5 here)
  43. Hi 1
  44. Hi 2
  45. Hi 3
  46. Hi 4
  47. Hi 5
  48.   Notice that, without the blocks, like this:
  49.     for(i=1;i<=n;i++) 
  50.         printf("Hi ");
  51.         printf("#%d\n", i);
  52. it will print "Hi " n times and then print "#i" where i has the same
  53. value as n. If n equalled 5 you would get "Hi Hi Hi Hi Hi 5".
  54.     
  55.     I'm afraid I find this difficult to explain succinctly; I hope
  56. these examples illustrate what you are seeing.  Perhaps you should dig
  57. in your textbook a bit more...
  58.  
  59. CKK> I just want to find out why the compiler does not catch it during
  60. CKK> compilation. It is compiled with UNIX gcc.
  61.   Well, frankly, it's not the compiler's job.  More importantly,
  62. neither would it always be correct to flag that.  Consider this:
  63.   for (i=0; ((i<ARRAY_MAX)&&(array[i]!=42)); i++)  /* DO NOTHING */ ;
  64.     printf("The Answer to Life is at array position %d\n", i);
  65.   This fragment uses the loop to find the position in the array which
  66. holds the value 42; the loop need not execute a statement, and the
  67. side effect is that when the loop terminates, i has the value we are
  68. interested in.
  69.  
  70. Hope this helps explain...
  71.  
  72.  
  73. --
  74. --Greg Owen
  75.     gowen@forte.cs.tufts.edu, gowen@jade.tufts.edu
  76. "C Spot. C Spot run. C Spot run without a core dump! Run, Spot, Run!"
  77.