Function 'for' and 'while' in C++

  • Context: Comp Sci 
  • Thread starter Thread starter AllenHe
  • Start date Start date
  • Tags Tags
    C++ Function
Click For Summary

Discussion Overview

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.

Discussion Character

  • Conceptual clarification
  • Debate/contested
  • Homework-related
  • Technical explanation

Main Points Raised

  • Some participants clarify that 'for' and 'while' are keywords, not functions, in C and C++.
  • One participant expresses uncertainty about the difference between 'for' and 'while', noting that both can be used to sum numbers but highlighting a suggestion that 'while' is preferable when the range is unknown.
  • Another participant questions whether the structure of 'for' and 'while' loops needs to be rewritten for different ranges, prompting further exploration of their flexibility.
  • Some argue that both loops can achieve the same outcomes, with the primary distinction being their syntax.
  • One participant suggests that 'for' loops are more suitable for known iterations, while 'while' loops are better for situations with unknown termination conditions, such as reading files.
  • Examples of both loop types are provided, illustrating their respective applications in converting decimal numbers to binary and reading file contents.

Areas of Agreement / Disagreement

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.

Contextual Notes

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.

Who May Find This Useful

This discussion may be useful for programming students, educators, and individuals interested in understanding loop constructs in C++ and their practical applications.

AllenHe
Messages
74
Reaction score
0

Homework Statement


What is the difference between these two functions?


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
So, what ideas do you have? If you don't say what you know, we can't offer advice on how you might learn more. If you are just looking for someone to write out an answer for you, you are on the wrong forum. I see you have 30 posts so you surely know by now that folks here will go out of their way to help you understand something, but no one is likely to be interested in just doing your work for you.
 
First off, for and while are not functions - they are keywords in C, C++, and other programming languages that are based on C.
 
o,sorry for that.
I know that you can use both for and while to find the sum of two numbers, like 0-10,which is 55.But I don't know the difference between these two,and someone told me that you can use 'while' when the range of the number is not given. I don't understand that.
 
Use your example of 0-10 adding up the digits to be 55. Do you have to rewrite your for loop to solve the same thing for say 0-15? 0-50? 0-100?

Would you want to use this for loop for 0-10 as a function that accepts different values of X, where x=10 means sum up all the digits up to 10 and x=15 means sum up all the digits up to 15?

Now if you solved this for a while loop. Would you nee to rewrite your while loop for different values ? 0-15, 0-50, 0-100

Would you want to use this while loop for a function that accepts different values of X?
 
AllenHe said:
But I don't know the difference between these two
Can you write down an example of each?

Then, can you state all of the features of each example?

Then, can you State what you think each example does?
 
You can choose freely if you want to use for or while. They are equivalent. The only difference is their syntax.
 
#include<iostream>
using namespace std;
int main()
{
int a=0,b=1;
while(b<=10){
a+=b;
++b;
}
count<<a<<endl;
}
This will find the sum from 1 to 10.The computer will do the command in the curly brackets as long as int b obeys the condition(b<=10)

#include<iostream>
using namespace std;
int main()
{int a=0;
for(b=1;b<=10;++b)
a+=b;
count<<a<<endl;
}
this has the same function.The computer will check whether the initial value of b satisfies the condition,if it does, it will perform the command a+=b; the complete ++b)until b doesn't satisfy the condition.
if both can do the same thing, then what's the need of having both 'for' and 'while'?
 
Because for loops have syntax that makes it easy to use for situations when you know they need to run a specific number of times (like if you're going through a set-length array), while while loops are better for situations when you don't know when the sequence will need to break (like if you're reading a file).

It's true that you can use both for the same cases. You can also use goto and conditionals to do the same thing as well (but don't). Keep in mind that you're working in an abstract environment by using a programming language, so there are several ways to do the same basic tasks and each has been designed to apply to specialized situations to make it easier on the programmer. I mean if you really want to strip things down to barebones, wait until you're working with assembly.
 
  • #10
oh, can you give me an example?(for 'for' and 'while').so you mean that it doesn't matter which ways I use, as long as I get the right results, yea?
 
  • #11
Pretty much, yeah.

Here's an example of a for loop application, converting a decimal number to 8-bit binary and storing the result in an array of 1s and 0s (in C, though it shouldn't be much different):

Code:
int dec = <some integer>;
int bin[8];
int i;
for (i = 0; i < 8; i++) {
	bin[i] = dec%2;
	dec /= 2;
}

A for loop is more appropriate because you're always going to go through it 8 times, no matter what dec is.

And here's a while loop reading in a file and printing the contents (also in C):

Code:
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
}

Though that would read better as a do-while:

Code:
FILE *fp;
fp = fopen(<some file>);
int c = 0;
do {
	c = getc(fp);
	putchar(c);
} while (c != EOF);

Which just means do the loop once before checking to see if you should. In this case, a while loop is more appropriate because the file's size isn't specified.
 
Last edited:

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K