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

• Comp Sci

## Homework Statement

What is the difference between these two functions?

## The Attempt at a Solution

phinds
Gold Member
2021 Award
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.

Mark44
Mentor
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?

Hurkyl
Staff Emeritus
Gold Member
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;
}
cout<<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;
cout<<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.

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?

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;
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: