AllenHe
- 74
- 0
Homework Statement
What is the difference between these two functions?
The discussion centers around the differences between 'for' and 'while' loops in C++, exploring their syntax, use cases, and functionality. Participants engage in a conceptual examination of these programming constructs, with a focus on their applications in various scenarios.
Participants express differing views on the necessity of having both 'for' and 'while' loops, with some asserting they are interchangeable while others emphasize their contextual appropriateness. The discussion remains unresolved regarding the optimal use cases for each loop type.
Some participants highlight that the choice between 'for' and 'while' may depend on specific programming scenarios, but there is no consensus on a definitive guideline for their use.
This discussion may be useful for programming students, educators, and individuals interested in understanding loop constructs in C++ and their practical applications.
Can you write down an example of each?AllenHe said:But I don't know the difference between these two
int dec = <some integer>;
int bin[8];
int i;
for (i = 0; i < 8; i++) {
bin[i] = dec%2;
dec /= 2;
}
FILE *fp;
fp = fopen(<some file>);
int c = getc(fp); //initialize c as the first character in the input file
while (c != EOF) { //Keep the loop going until c == EOF (end-of-file).
putchar(c); //print whatever c is, as a character (c is considered an int, but it has a char representation).
c = getc(fp); //set c as the next character in the file
}
FILE *fp;
fp = fopen(<some file>);
int c = 0;
do {
c = getc(fp);
putchar(c);
} while (c != EOF);