C: Simple for loop is not working

  • Thread starter Thread starter mbrmbrg
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around a participant's issue with a simple "for" loop in C programming that is intended to print "Hello, World!" five times. The focus is on understanding the behavior of the loop and the placement of the return statement within the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Exploratory

Main Points Raised

  • A participant shares their code and describes that it only runs for one iteration due to the placement of the return statement within the loop.
  • Another participant suggests considering the position of the return statement and its implications on the loop's execution.
  • There is a discussion about what the return statement does, with some participants noting that it returns control to the caller, typically the operating system.
  • A participant expresses confusion about why the program encounters the return statement before completing all iterations of the loop.
  • Comparative examples are provided to illustrate the difference between having a return statement inside the loop versus outside of it.
  • A later reply reveals that the participant realized their mistake of placing the return statement inside the loop, which explains the unexpected behavior.
  • Participants express camaraderie and understanding regarding common mistakes in programming.

Areas of Agreement / Disagreement

Participants generally agree on the importance of the return statement's position in relation to the loop, and there is a shared understanding of common programming errors. However, there is no explicit consensus on the broader implications of return statements beyond the immediate context of the loop.

Contextual Notes

The discussion highlights the importance of code structure and the potential for misunderstanding basic programming concepts, particularly for beginners. There are unresolved questions about the broader implications of return statements in different contexts.

Who May Find This Useful

Individuals learning C programming, particularly those struggling with control flow and loop structures, may find this discussion beneficial.

mbrmbrg
Messages
486
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: 428
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 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K