"Bill Cunningham" <nospam@[EMAIL PROTECTED]
> writes:
> "Robert Gamble" <rgamble99@[EMAIL PROTECTED]
> wrote in message
> news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@[EMAIL PROTECTED]
>
> [snip]
>
>
>> I think you want braces around these statements, yes?
>
> I don't know. I've ran code before and seen a lot of people use
while
> and statements after it and the code works. No braces. So I usually use
it
> like that. It's not a typo I don't usually use braces with while.
[...]
Do you understand what the braces mean?
Here's the tail end of the code Robert was commenting on:
[...]
while(a!=EOF)
a=fgetc(ifp);
fputc(a,ofp);
printf("done\n");
return 0;}
Putting the closing brace immediately after the semicolon like that is
horribly bad style. Putting everything at the same indentation level
is also horribly bad style. The compiler doesn't care, but anyone
trying to read your code does.
The syntax of a while statement is:
while ( expression ) statement
Note that that's a *single* statement. If you want the body to be
multiple statements, you need to wrap them in braces to create a
compound statement, which is itself as single statement. (The same
thing applies to if statements; you correctly used braces for those,
so I don't know why the while loop was such a problem.)
The code I quoted is equivalent to this:
[...]
while (a != EOF)
a = fgetc(ifp);
fputc(a, ofp);
printf("done\n");
return 0;
}
With proper indentation, you can see that the while controls only a
single statement, ``a = fgetc(ifp);''. The fputc call will occur
exactly once, after the while loop terminates. I can't be certain,
but I *think* you wanted both the fgetc and fputc calls to be in the
body of the loop. To do that, you need braces:
[...]
while (a != EOF) {
a = fgetc(ifp);
fputc(a, ofp);
}
printf("done\n");
return 0;
}
In my opinion, it's a good habit always to use braces even if you only
want only a single statement in the body. It makes the code more
consistent and easier to maintain if you later want to add a second
statement. (Others will disagree.)
Judicious use of whitespace, especially around operators and after
commas, also makes the code much easier to read.
From now on, if you post a chunk of nearly illegible code like what
you posted upthread, with no indentation and virtually no whitespace,
I will ask you to format it properly before I'll even consider helping
you fix any other problems.
--
Keith Thompson (The_Other_Keith) <kst-u@[EMAIL PROTECTED]
>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


|