Is this the correct basic code for a for-loop?

  • Thread starter Thread starter tandoorichicken
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Homework Help Overview

The discussion revolves around the correct syntax and structure of a for-loop in programming, specifically addressing common mistakes and formatting preferences.

Discussion Character

  • Exploratory, Conceptual clarification, Problem interpretation

Approaches and Questions Raised

  • Participants discuss the use of commas versus semicolons in the for-loop syntax, with some providing corrected examples. There is also a question about how to format output in a loop to achieve a specific result, such as leaving a blank line after printing.

Discussion Status

Several participants have offered guidance on the correct syntax for the for-loop, emphasizing the use of semicolons. There is an ongoing exploration of formatting preferences and output structure, with some suggestions provided for achieving the desired output.

Contextual Notes

Participants express differing opinions on coding style, particularly regarding the placement of braces in the code. Additionally, there is a specific programming homework context that involves printing output in a certain format.

tandoorichicken
Messages
245
Reaction score
0
Is this the correct basic code for a for-loop?

for (n=1, n<=10, n++) {
/* statements */
}

If not what am I doing wrong?
 
Physics news on Phys.org
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++)
 
thank you very, very much. :biggrin:
 
HallsofIvy said:
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 don't know why many of the books promote that style!
I always had it ur way because as u put it , it looks balanced not to mention neat!

-- AI
 
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 doesn't 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
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 28 ·
Replies
28
Views
2K
  • · Replies 48 ·
2
Replies
48
Views
3K