while( something OR something )

This is a discussion on while( something OR something ) within the C Programming forums, part of the General Programming Boards category; Hi there, Just a quickie I hope... I am using a while clause: Code: while (fabs(it1-r1)<t OR fabs(it1-r2)<t) { it1=(it1-(((a*(it1*it1))+(b*it1)+c)/((2*a*it1)+(b)))); ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    9

    while( something OR something )

    Hi there,
    Just a quickie I hope...
    I am using a while clause:

    Code:
      while (fabs(it1-r1)<t OR fabs(it1-r2)<t)  
      {
        it1=(it1-(((a*(it1*it1))+(b*it1)+c)/((2*a*it1)+(b))));
      }
    And want it to keep go until either one thing happens or another thing happens. I have been searching through my help files and can't find a way of doing this...plus there's a problem searching 'or' through the forums as its too common a word...!! Thank you!
    Last edited by aligeb; 05-02-2007 at 02:36 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    The operator you are looking for is "||"

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    like this:

    while ((condition >= 5) || (AnotherCondition != 2))

    The loop will continue on until both statements are false. && is for "and" and the loop will continue until one of the elements is false. More complex conditions have different variations.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    while ((condition >= 5) || (AnotherCondition != 2))

    The loop will continue on until both statements are false. && is for "and" and the loop will continue until one of the elements is false. More complex conditions have different variations.
    Sorry...so just to clarify...
    || waits until both statements are false
    && waits until one statement is false

    Is that correct?
    Thanks

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by aligeb View Post
    Sorry...so just to clarify...
    || waits until both statements are false
    && waits until one statement is false
    It doesn't "wait," it evaluates. '&&' is true if and only if both the left and the right are true. '||' is true if EITHER the left or right is true, or if both are true.

    Furthermore, the operators are "short circuiting," which means they stop evaluating as soon as the result is known. For instance, the expression "func1() && func2()" will not evaluate func2() if func1() is false, since at that point it is already known that the entire expression is false. And "func1() || func2()" will not evaluate func2() if func1() is true, since again, at that point it is known that the result is true.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    Ahhh
    Thanks very much! All sorted now!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There is a tutorial that covers this, though not in great depth: http://www.cprogramming.com/tutorial/c/lesson2.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21