C: Simple for loop is not working

  • Thread starter mbrmbrg
  • Start date
  • Tags
    Loop
In summary, the for loop in the code example does not seem to be working, and it may be because of a mistake in placement of the return statement.
  • #1
mbrmbrg
496
2
C: Simple "for" loop is not working

Homework Statement



I'm taking an online course (yay lynda!) in C/C++, and I am trying to replicate a course example that prints "Hello, World!" five times, using a simple "for" loop.

Homework Equations



for(int i = 1; i <= 5; ++i){...}

The Attempt at a Solution



// working.c by Bill Weinman <http://bw.org/>
#include <stdio.h>

int main( int argc, char ** argv ) {
for(int i = 1; i <= 5; ++i) {
int x = printf("Hello, World!\n") * 5;
printf("printf returned %d\n", x);
printf("%d", i);
return 0;
}
}
My program runs, but only with 1 iteration of the loop. For reading ease, I've attached a screenshot taken in Eclipse.

4. Other Potentially Relevant Information

I previously have had trouble with a dummy program that required user input. It had been a long day, so I kind of closed my eyes and pretended it was going to be fine, but I never solved that problem, either.

5. Further Work

Uninstall and reinstall everything?? I'd really rather not, but it's all I've got right now.

6. Thank you!
 

Attachments

  • stalled loop.JPG
    stalled loop.JPG
    56.4 KB · Views: 374
Last edited by a moderator:
Physics news on Phys.org
  • #2


Code:
#include <stdio.h>

int main( int argc, char ** argv ) {
	for(int i = 1; i <= 5; ++i) {
		int x = printf("Hello, World!\n") * 5;
		printf("printf returned %d\n", x);
		printf("%d", i);
		return 0;
	}
}

Use [noparse]
Code:
[/noparse] tags to show your code.

Think about position of the return statement. What it does?
 
  • #3


Borek said:
Use [noparse]
Code:
[/noparse] tags to show your code.

Thanks, that's much cleaner!

Think about position of the return statement. What it does?

The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?
 
  • #4


mbrmbrg said:
The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?

What does the "return" alone do?

As its name implies.
 
  • #5


mbrmbrg said:
The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?
A return statement in main returns control to whoever called main, which is usually the operating system.
 
  • #6


Return brings you back to what comes next. (I know that's convoluted, but it's the only way I can think of to express that return generally moves you along, but as its name implies, return can also bring you back to the beginning.)

But why would the program encounter the return line at all before the loop has run all of its iterations?
 
  • #7


mbrmbrg said:
But why would the program encounter the return line at all before the loop has run all of its iterations?

Compare:

Code:
for (i = 0;i != 5;i++)
{
   printf("%s","Hello World\n");
   return;
}

and

Code:
for (i = 0;i != 5;i++)
{
   printf("%s","Hello World\n");
}
return;

Do you see the difference?
 
  • #8


Oh. My. Gosh. I put the line that ends my program INSIDE my for loop! No wonder it ends after 1 iteration!

Thank you!

(My apologies for exclamation point abuse; I get overly excited by understanding stuff.)
 
  • #9


We all did such things at some point, welcome to the club :wink:
 
  • #10


:smile:
 

Related to C: Simple for loop is not working

1. Why is my for loop not working in C?

There could be several reasons why your for loop is not working in C. Some common reasons include incorrect syntax, logical errors in the loop condition, or incorrect use of loop variables. It is important to carefully review your code and check for any mistakes or bugs that may be causing the loop to malfunction.

2. How do I fix a for loop that is not working in C?

To fix a for loop that is not working in C, you will need to identify and correct the error in your code. This could involve checking for any syntax errors, making sure the loop condition is correct, or reassigning loop variables if necessary. Debugging tools such as printf statements can also be helpful in finding and fixing errors in your for loop.

3. Can a for loop be used in C to iterate through an array?

Yes, a for loop can be used in C to iterate through an array. You can use the loop counter variable to access each element of the array, which allows you to perform operations on the elements as needed. It is important to ensure that the loop condition and increment are set up correctly to avoid any errors or infinite loops.

4. What is the syntax for a for loop in C?

The basic syntax for a for loop in C is:
for(initialization; condition; increment) {
// code to be executed
}

The initialization step initializes the loop counter variable, the condition is evaluated before each iteration to determine if the loop should continue, and the increment step updates the loop counter variable after each iteration. The code within the curly braces will be executed as long as the condition is true.

5. Can a for loop in C be nested?

Yes, a for loop in C can be nested, meaning that one for loop can be placed inside another for loop. This can be useful for iterating through multidimensional arrays or performing more complex iterations. It is important to ensure that the loop conditions and increments are set up correctly to avoid any errors or infinite loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
985
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top