Thread: do while loop

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    do while loop

    Whats wrong with this loop?
    I cant proceed with the program even when S or R is input.
    I can exit if I take the || out but I need to repeat untill I get a R or a S.

    Code:
    do
    		{
    		printf ("Enter R for Required or S for Suggested: ");
    		while (getchar() != '\n');
    		reqOrSugg = getchar();
    		printf ("You entered %c\n", reqOrSugg);
    		}
    while (reqOrSugg != 'R' || 'S');

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Methinks you want this:
    Code:
    while ( reqOrSugg != 'R' && reqOrSugg != 'S' );
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The reason you have to do what Dave_Sinkula said and cannot do
    Code:
    while (foo != 'A' || 'B')
    is because that expression is effectively
    Code:
    foo != ('A' || 'B')
    Since 'A' is non-zero, it becomes
    Code:
    foo != 1
    This is clearly not what you want.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    Thanks Again Guys

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cwr
    The reason you have to do what Dave_Sinkula said and cannot do
    Code:
    while (foo != 'A' || 'B')
    is because that expression is effectively
    Code:
    foo != ('A' || 'B')
    Since 'A' is non-zero, it becomes
    Code:
    foo != 1
    This is clearly not what you want.
    Doesn't the != bind more tightly than the ||, thus making the equivalent like this?
    Code:
    (foo != 'A') || 'B'
    The second part will always be true.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Dave_Sinkula
    Doesn't the != bind more tightly than the ||, thus making the equivalent like this?
    Code:
    (foo != 'A') || 'B'
    Yes of course, apologies, thanks for pointing this out.

    When I made the post I quickly and sleepily looked at the list below and saw |= as !=

    Code:
    Operator                             Associativity
    --------                             -------------
    () [] -> .                           left to right
    ! ~ ++ -- - (type) * & sizeof        right to left
    * / %                                left to right
    + -                                  left to right
    << >>                                left to right
    < <= > >=                            left to right
    == !=                                left to right
    &                                    left to right
    ^                                    left to right
    |                                    left to right
    &&                                   left to right
    ||                                   left to right
    ?:                                   right to left
    = += -= *= /= %= <<= >>= &= ^= |=    right to left
    ,                                    left to right

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    Just out of curiosity

    I am really new to C and my instructor does not do a great job explaining things. Wondering if one of you can clarify this for me.

    I need to keep asking for input untill I get a R or S.

    Code:
    while ( reqOrSugg != 'R' && reqOrSugg != 'S' );
    This looks to me like repeat the loop untill the reqOrSugg is equal to BOTH R and S. How can this work because the variable reqOrSugg can not be R and S? This cant be the case because it is what made the program work.

    Code:
    while ( reqOrSugg != 'R' || reqOrSugg != 'S' );
    This looks like the code that should work to me because reqOrSugg can be equal to EITHER R or S. Only one needs to be satisfied to exit the loop...

    I guess there is something I dont understand aboutthe and(&&) and or(||) operators so could one of you explain this?

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    1
    while ( reqOrSugg != 'R' || reqOrSugg != 'S' );
    Should Be
    while ( reqOrSugg != 'R' || reqOrSugg != 'S' ) <- colon not needed for loops

    James

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Throw a couple of values at the expression like 'R', 'S', and 'T'. Mentally do the comparisons and get your result. Or write a little bit of code to help.
    Code:
    #include <stdio.h>
    
    void foo(char reqOrSugg)
    {
       printf("'%c' != 'R' && '%c' != 'S' => ", reqOrSugg, reqOrSugg);
       printf("%d && %d = %d\n", reqOrSugg != 'R', reqOrSugg != 'S',
              reqOrSugg != 'R' && reqOrSugg != 'S');
    }
    
    void bar(char reqOrSugg)
    {
       printf("'%c' != 'R' || '%c' != 'S' => ", reqOrSugg, reqOrSugg);
       printf("%d || %d = %d\n", reqOrSugg != 'R', reqOrSugg != 'S',
              reqOrSugg != 'R' || reqOrSugg != 'S');
    }
    
    int main (void)
    {
       foo('R');
       foo('S');
       foo('T');
       bar('R');
       bar('S');
       bar('T');
       return 0;
    }
    
    /* my output
    'R' != 'R' && 'R' != 'S' => 0 && 1 = 0
    'S' != 'R' && 'S' != 'S' => 1 && 0 = 0
    'T' != 'R' && 'T' != 'S' => 1 && 1 = 1
    'R' != 'R' || 'R' != 'S' => 0 || 1 = 1
    'S' != 'R' || 'S' != 'S' => 1 || 0 = 1
    'T' != 'R' || 'T' != 'S' => 1 || 1 = 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by skripti
    while ( reqOrSugg != 'R' || reqOrSugg != 'S' ) <- colon not needed for loops
    Look up a do...while.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
     // while var is not 'R' and var is not 'S'
    while ( reqOrSugg != 'R' && reqOrSugg != 'S' )
    or
    Code:
     // while not ( var is 'R' or var is  'S' )
    while ( !( reqOrSugg == 'R' ||  reqOrSugg == 'S' ))
    does the same ( break the loop if either 'R' or 'S' is entered )

    Kurt
    Last edited by ZuK; 01-27-2006 at 01:27 PM.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Remember, the loop is running until either S or R is entered. So you want the loop to continue while the input was not S and the input was not R. If you used or, the loop would run forever, since to end the loop, the input would have to be S and R at the same time, which is impossible.

    Code:
    while ( reqOrSugg != 'R' || reqOrSugg != 'S' ) <- colon not needed for loops
    And it's a semi-colon, too.
    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

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM