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

  • Comp Sci
  • Thread starter AllenHe
  • Start date
  • #1
74
0

Homework Statement


What is the difference between these two functions?


Homework Equations





The Attempt at a Solution

 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
16,726
7,404
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.
 
  • #3
34,678
6,387
First off, for and while are not functions - they are keywords in C, C++, and other programming languages that are based on C.
 
  • #4
74
0
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.
 
  • #5
8
0
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?
 
  • #6
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
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?
 
  • #7
You can choose freely if you want to use for or while. They are equivalent. The only difference is their syntax.
 
  • #8
74
0
#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'?
 
  • #9
3
0
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
74
0
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
3
0
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:

Related Threads on Function 'for' and 'while' in C++

Replies
1
Views
1K
  • Last Post
Replies
7
Views
1K
  • Last Post
Replies
2
Views
2K
Replies
4
Views
891
  • Last Post
Replies
5
Views
5K
Replies
5
Views
4K
Replies
10
Views
976
  • Last Post
Replies
4
Views
4K
Top