home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!math.fu-berlin.de!hamel!rene
- From: rene@hamel.uucp (Rene Mueller)
- Subject: Re: If statement question
- Message-ID: <TJ78YGQ@math.fu-berlin.de>
- Sender: rene@hamel (Rene Mueller)
- Organization: Free University of Berlin, Germany
- References: <GOWEN.92Nov22235007@jade.tufts.edu>
- Distribution: na
- Date: Mon, 23 Nov 1992 23:16:24 GMT
- Lines: 71
-
- In article <GOWEN.92Nov22235007@jade.tufts.edu>, gowen@jade.tufts.edu (G. Lee Owen) writes:
- |>
- |>
- |> Sorry, took me a day extra to get this out, but here we go...
- |>
- |> From ckkoo@magnus.acs.ohio-state.edu (Chee K Koo) on 20 Nov 92:
- |> CKK> I am new to C programming, while i was doing a numerical analysis
- |> CKK> program which uses many for loop
- |> CKK> here is the loop looks like
- |> CKK> for(i=1;i<=n;i++);
- |> CKK> {
- |> CKK> }
- |> CKK> i put the ";" after the loop header, when the program is excuted, the
- |> CKK> loop is skipped, however, it works fine when the ";" is removed.
- |>
- |> When you say 'for(i=1;i<=n;i++);' you are really telling the
- |> compiler 'for(i=1;i<=n;i++) /* DO NOTHING */;'. The for loop will
- |> work on the (single) statement following it OR on the block following
- |> it, where { and } mark off the block.
- |> If you were writing a loop that would execute one statement, it
- |> would be like this, right?
- |> for(i=1;i<=n;i++) printf("Hi #%d\n", i);
- |> Notice how there is no ; between the end of the loop parens and the
- |> printf statement. Well, the same thing goes for a for loop with a
- |> block following it. Say you had to execute two statements, you would
- |> put them in a block:
- |> for(i=1;i<=n;i++)
- |> {
- |> printf("Hi ");
- |> printf("#%d\n", i);
- |> }
- |> like such. The output of this would be (n == 5 here)
- |> Hi 1
- |> Hi 2
- |> Hi 3
- |> Hi 4
- |> Hi 5
- |> Notice that, without the blocks, like this:
- |> for(i=1;i<=n;i++)
- |> printf("Hi ");
- |> printf("#%d\n", i);
- |> it will print "Hi " n times and then print "#i" where i has the same
- Ok so far...
- |> value as n. If n equalled 5 you would get "Hi Hi Hi Hi Hi 5".
- That was wrong!
- Your condition was i<=n, so i==n will be executed, then i++ and THEN the loop
- will be breaked...
- That means the output is:
- "HI HI HI HI HI 6"!!!
-
- |>
- |> I'm afraid I find this difficult to explain succinctly; I hope
- |> these examples illustrate what you are seeing. Perhaps you should dig
- |> in your textbook a bit more...
- |>
- |> CKK> I just want to find out why the compiler does not catch it during
- |> CKK> compilation. It is compiled with UNIX gcc.
- |> Well, frankly, it's not the compiler's job. More importantly,
- |> neither would it always be correct to flag that. Consider this:
- |> for (i=0; ((i<ARRAY_MAX)&&(array[i]!=42)); i++) /* DO NOTHING */ ;
- |> printf("The Answer to Life is at array position %d\n", i);
- |> This fragment uses the loop to find the position in the array which
- |> holds the value 42; the loop need not execute a statement, and the
- |> side effect is that when the loop terminates, i has the value we are
- |> interested in.
- Sorry, but here is the same mistake...
- You must FIRST decrement(increment?? grmbl) SUBSTRACT 1 from i to get the
- correct array position:
- printf("The Answer to Life is at array position %d\n", --i);
-
- Hope there will be nobody confused to much...
-