• Support PF! Buy your school textbooks, materials and every day products Here!

C: Simple for loop is not working

  • Thread starter mbrmbrg
  • Start date
  • #1
486
1
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/> [Broken]
#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 gonna 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

Last edited by a moderator:

Answers and Replies

  • #2
Borek
Mentor
28,298
2,683


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
486
1


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
Borek
Mentor
28,298
2,683


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
33,084
4,791


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
486
1


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
Borek
Mentor
28,298
2,683


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
486
1


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
Borek
Mentor
28,298
2,683


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


:smile:
 

Related Threads for: C: Simple for loop is not working

Replies
3
Views
1K
Replies
4
Views
782
Replies
1
Views
1K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
2K
Replies
3
Views
518
  • Last Post
Replies
7
Views
1K
Replies
13
Views
1K
Top