Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Oct 25, 2011 #1
    1. The problem statement, all variables and given/known data
    What is the difference between these two functions?

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Oct 25, 2011 #2


    User Avatar
    Gold Member

    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.
  4. Oct 25, 2011 #3


    Staff: Mentor

    First off, for and while are not functions - they are keywords in C, C++, and other programming languages that are based on C.
  5. Oct 26, 2011 #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.
  6. Oct 26, 2011 #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?
  7. Oct 26, 2011 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
  8. Oct 27, 2011 #7
    You can choose freely if you want to use for or while. They are equivalent. The only difference is their syntax.
  9. Oct 27, 2011 #8
    using namespace std;
    int main()
    int a=0,b=1;
    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)

    using namespace std;
    int main()
    {int a=0;
    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'?
  10. Oct 27, 2011 #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.
  11. Oct 27, 2011 #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?
  12. Oct 27, 2011 #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 (Text):
    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 (Text):
    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 (Text):
    FILE *fp;
    fp = fopen(<some file>);
    int c = 0;
    do {
        c = getc(fp);
    } 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: Oct 27, 2011
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook