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

  • Thread starter tandoorichicken
  • Start date
  • Tags
    Code
In summary, the correct basic code for a for-loop should use semi-colons instead of commas in the statement, and the opening curly brace should be on the same line as the "for" statement. Additionally, to print a blank line at the end, you can use a conditional statement within the for-loop or simply place a printf statement after the loop.
  • #1
tandoorichicken
245
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
  • #2
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.)
 
  • #3
I think you want semicolons separating items in the "for" satement"

for (i = 1; n <= 0; n++)
 
  • #4
thank you very, very much. :biggrin:
 
  • #5
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
 
  • #6
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?
 
  • #7
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
 

1. What is a for-loop?

A for-loop is a programming structure that allows you to execute a block of code repeatedly for a specified number of times or until a certain condition is met.

2. How do you write a for-loop?

To write a for-loop, you need to specify the initial value, the condition for the loop to continue, and the increment or decrement value. For example: for(i=0; i<5; i++) { //code to be executed }

3. What is the purpose of a for-loop?

The purpose of a for-loop is to automate repetitive tasks and make the code more efficient. It allows you to iterate through a set of data or execute a block of code multiple times without having to write the code multiple times.

4. What is the difference between a for-loop and a while-loop?

The main difference between a for-loop and a while-loop is that a for-loop is used when the number of iterations is known beforehand, whereas a while-loop is used when the number of iterations is not known and is dependent on a condition.

5. What happens if the condition in a for-loop is never met?

If the condition in a for-loop is never met, the loop will not be executed and the code will continue to execute after the loop. This can result in the loop not being executed at all or only being executed once, depending on the condition.

Similar threads

  • Introductory Physics Homework Help
Replies
8
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
274
  • Introductory Physics Homework Help
Replies
28
Views
572
  • Programming and Computer Science
Replies
14
Views
2K
Replies
8
Views
484
  • Introductory Physics Homework Help
Replies
6
Views
597
  • Introductory Physics Homework Help
Replies
4
Views
702
  • Introductory Physics Homework Help
Replies
2
Views
186
  • Introductory Physics Homework Help
Replies
11
Views
763
  • Engineering and Comp Sci Homework Help
Replies
3
Views
503
Back
Top