C: Simple for loop is not working

  • Thread starter Thread starter mbrmbrg
  • Start date Start date
  • Tags Tags
    Loop
AI Thread Summary
The issue with the "for" loop not working correctly is due to the placement of the return statement inside the loop, which causes the program to exit after the first iteration. The return statement in the main function ends its execution and returns control to the operating system, preventing further iterations. By moving the return statement outside the loop, the program can complete all five iterations as intended. The discussion highlights the importance of understanding control flow in programming. This realization leads to a successful resolution of the problem.
mbrmbrg
Messages
485
Reaction score
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: 407
Last edited by a moderator:
Physics news on Phys.org


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?
 


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?
 


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.
 


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.
 


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?
 


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?
 


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.)
 


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


:smile:
 

Similar threads

Replies
12
Views
2K
Replies
3
Views
1K
Replies
12
Views
2K
Replies
1
Views
10K
Replies
8
Views
2K
Replies
4
Views
1K
Replies
2
Views
2K
Back
Top