if statement within while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BevG
    New Member
    • Apr 2007
    • 12

    if statement within while loop

    I would like to test a condition (using "if") on each recurrence of a while loop, but once the condition has been fulfilled the first time, it is bypassed for the rest of the while loop.
    eg.
    x = 0.1;
    while (x < 1.3) {
    if (x == 0.9 || x == 0.5) {
    cout << "Helloooooo !!!" << endl;
    }
    x = x + 0.1;

    }
    It prints "Helloooooo !!!" once only, at x = 0.5, not again at x = 0.9.

    Is there a way to get it to evaluate the condition every time?
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by BevG
    I would like to test a condition (using "if") on each recurrence of a while loop, but once the condition has been fulfilled the first time, it is bypassed for the rest of the while loop.
    eg.
    x = 0.1;
    while (x < 1.3) {
    if (x == 0.9 || x == 0.5) {
    cout << "Helloooooo !!!" << endl;
    }
    x = x + 0.1;

    }
    It prints "Helloooooo !!!" once only, at x = 0.5, not again at x = 0.9.

    Is there a way to get it to evaluate the condition every time?
    This probably has something to do with the fact that you are using floats to do your looping instead of ints. In some systems when you increment by a float value you get something akin to x = x + 0.0999999999 which is not exactly 0.1 but still pretty close. Try doing it with ints and let me know how it works.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Hmm...I'm getting the same result. I think this is due to roundoff error - your x is about 0.9, but most likely it is actually 0.8999999999992 or 0.900000000001 or some other fraction absurdly close to 0.9. Since this isn't exactly 0.9, the if statement doesn't execute.

      When trying to compare double (or float) values, you should instead check if the number is close to your target - close being within 0.000000001 or some appropriately small value. You can write your own function for this.

      Comment

      • BevG
        New Member
        • Apr 2007
        • 12

        #4
        Originally posted by RedSon
        This probably has something to do with the fact that you are using floats to do your looping instead of ints. In some systems when you increment by a float value you get something akin to x = x + 0.0999999999 which is not exactly 0.1 but still pretty close. Try doing it with ints and let me know how it works.
        Thanks for that! Yes, it works with integers. I have to do it with floats though. I've just tried it with this code:

        x = 0.1;
        while (x < 1.3) {
        if ((x < 0.905 && x > 0.895) || (x > 0.495 && x < 0.505)) {
        cout << "Helloooooo !!!" << endl;
        }
        x = x + 0.1;
        cout << x << endl;
        }

        and it does the job, but it seems a bit clumsy ... ? Do you have any ideas, or is there maybe another approach to take altogether?

        To give some context: what I'm actually doing (having written this bit of code simply to isolate the problem I was having) is computing values of y for values of x incrementing by 0.1, for example, but storing only particular ones in an array, using the "if" loop to pick up the values I want to store.

        Comment

        • BevG
          New Member
          • Apr 2007
          • 12

          #5
          Ganon11, thanks - I saw your reply only after I'd submitted mine!
          So no simpler way, then?

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Why do you have to do it with floats?

            Comment

            • BevG
              New Member
              • Apr 2007
              • 12

              #7
              Originally posted by RedSon
              Why do you have to do it with floats?
              It's the accuracy range I'm working in.

              It's a numerical methods program that I'm actually writing, and I have to calculate function values at x starting with 0.0 and incrementing by 0.1 until x is 1.2, using the function value at each step to compute the next one. I have to repeat this process over the same interval with increment 0.01. The values I have to store are at 0.0, 0.2, etc, so I somehow have to isolate them in the middle of the computations.

              The code fragment is:

              while (x <= 1.2) {
              if (x == 0.2*save) {
              A[save][col] = y;
              save++;
              }
              if (x != 1.2) {
              derv = der(x,y);
              y = y + h*derv;
              x = x + h;
              }
              }

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                What about multiplying everything by 10 and converting to int? You can always divide back later.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Originally posted by BevG
                  Thanks for that! Yes, it works with integers. I have to do it with floats though. I've just tried it with this code:

                  x = 0.1;
                  while (x < 1.3) {
                  if ((x < 0.905 && x > 0.895) || (x > 0.495 && x < 0.505)) {
                  cout << "Helloooooo !!!" << endl;
                  }
                  x = x + 0.1;
                  cout << x << endl;
                  }

                  and it does the job, but it seems a bit clumsy ... ? Do you have any ideas, or is there maybe another approach to take altogether?
                  As I said, you can write your own short function to do this. Out of curiosity, I wrote a function to do this (it only took me 3 lines of code), which would make your code look like:

                  Code:
                          x = 0.1;
                          while (x < 1.3) {
                                  if (dequals(x, 0.9) || dequals(x, 0.5)) {
                                     cout << "Helloooooo!!!" << endl;
                                  }
                                  x = x + 0.1;
                                  cout << x << endl;
                          }
                  I called it dequals for doubleequals. It is a bool returning function, and returns true if the first argument is within EPS of the other, where EPS is a constant I defined to be about 0.000000001

                  Comment

                  • BevG
                    New Member
                    • Apr 2007
                    • 12

                    #10
                    [QUOTE=Ganon11]
                    Ah, I see ... will give it a bash.

                    Comment

                    • BevG
                      New Member
                      • Apr 2007
                      • 12

                      #11
                      All right, that seems to be working ... The accuracy isn't great, though, so that's the next thing I have to tackle, but this sorts the question on the if statement. Thanks! Bev

                      Comment

                      Working...