View Full Version : C Code
tandoorichicken
Oct13-04, 11:24 AM
Is this the correct basic code for a for-loop?
for (n=1, n<=10, n++) {
/* statements */
}
If not what am I doing wrong?
HallsofIvy
Oct13-04, 12:10 PM
Use semi-colons rather than commas!
for (int i=0;i<=10;i++)
{
}
(I cannot bring myself to put that first "{" up on the previous line! It looks so unbalanced.)
I think you want semicolons separating items in the "for" satement"
for (i = 1; n <= 0; n++)
tandoorichicken
Oct13-04, 12:44 PM
thank you very, very much. :biggrin:
TenaliRaman
Oct13-04, 12:59 PM
Use semi-colons rather than commas!
for (int i=0;i<=10;i++)
{
}
(I cannot bring myself to put that first "{" up on the previous line! It looks so unbalanced.)
Same thoughts here!!!!
i dunno why many of the books promote that style!!
I always had it ur way cuz as u put it , it looks balanced not to mention neat!!
-- AI
tandoorichicken
Oct13-04, 01:16 PM
Okay one more problem. For a programming homework I had to print my name ten times and leave a blank line at the end. My code looked like this:
for (k=1; k<=10; k++) {
printf("%2d Joe Schmoe\n", k);
}
That prints my name ten times but doesnt leave a blank line at the end. If I put two \n's then that leaves a blank line in between each printing. Is there a way to print a blank line at the end within the structure of the for-loop or do I have to just use printf("\n"); after the whole thing?
You could use something like
if (k==10) printf("\n");
but that would be silly. Just put the printf("\n"); at the end, after the for loop.
- Warren
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.