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

  • Comp Sci
  • Thread starter AllenHe
  • Start date
  • Tags
    C++ Function
In summary, the main difference between for and while loops is their syntax and the situations in which they are used. For loops are better for situations where the number of iterations is known beforehand, while while loops are better for situations where the number of iterations is not known beforehand. However, both can be used for the same tasks and it ultimately depends on the preference of the programmer.
  • #1
AllenHe
74
0

Homework Statement


What is the difference between these two functions?


Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
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
First off, for and while are not functions - they are keywords in C, C++, and other programming languages that are based on C.
 
  • #4
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
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
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?
 
  • #7
You can choose freely if you want to use for or while. They are equivalent. The only difference is their syntax.
 
  • #8
#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
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:

What is the difference between a 'for' loop and a 'while' loop in C++?

A 'for' loop and a 'while' loop are both used for repeating a block of code multiple times, but they differ in their syntax and usage. A 'for' loop is typically used when the number of iterations is known beforehand, and it consists of three parts: an initialization, a condition, and an increment/decrement. On the other hand, a 'while' loop is used when the number of iterations is not known beforehand, and it only has a condition that is checked before each iteration.

When should I use a 'for' loop over a 'while' loop in C++?

You should use a 'for' loop when you know the exact number of iterations that need to be performed, such as when iterating through an array or a fixed-size container. This helps to make the code more concise and readable. However, if the number of iterations is not known beforehand or if it depends on a condition, a 'while' loop would be more suitable.

Can I use a 'for' loop and a 'while' loop interchangeably in C++?

Yes, in most cases, a 'for' loop and a 'while' loop can be used interchangeably. However, it is important to choose the appropriate loop based on the requirements of the program to ensure efficient and bug-free code.

What is the syntax for a 'for' loop and a 'while' loop in C++?

The syntax for a 'for' loop in C++ is:

for (initialization; condition; increment/decrement) {

// code to be repeated

}

The syntax for a 'while' loop in C++ is:

while (condition) {

// code to be repeated

}

Can I use 'break' and 'continue' statements in both 'for' and 'while' loops in C++?

Yes, 'break' and 'continue' statements can be used in both 'for' and 'while' loops in C++. 'break' is used to terminate a loop and move on to the next statement outside the loop, while 'continue' is used to skip the current iteration and move on to the next one. However, it is important to use them carefully to avoid unexpected results and bugs in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top