home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17068 < prev    next >
Encoding:
Text File  |  1992-11-24  |  3.2 KB  |  84 lines

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