Thread: do - while loop problem

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    do - while loop problem

    hi every1

    im doing a do - while loop in c and to check that either 1, 2, 3 or 4 is entered and to keep on asking for those numbers till one of them is entered (do-while checks for that).

    the thing is the loop keeps on repeating even if one of those numbers is eneterd
    my condition is

    Code:
    while(gameCh != 1 || gameCh != 2 || gameCh != 3 || gameCh != 4);
    but when i just check for one number
    eg

    Code:
    while(gameCh != 1)
    the do while loop works - it exits when i enter 1
    why doesnt it work for the four options?
    could it be the multiple conditions in the first example?

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    The || has to be replaced with && as below,

    Code:
    while(gameCh != 1 && gameCh != 2 && gameCh != 3 && gameCh != 4);

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    while(gameCh != 1 || gameCh != 2 || gameCh != 3 || gameCh != 4);
    is an infinite loop if gameCh is not any one of: 1,2,3,4

    Code:
    while(gameCh != 1 || gameCh != 2 || gameCh != 3 || gameCh != 4){
    
    
           // put an exit condtion here or set  gameCh =1;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is an infinite loop if gameCh is not any one of: 1,2,3,4
    You can't be sure of this since the OP didn't include the body of his do..while loop.
    My best code is written with the delete key.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    while(gameCh != 1 || gameCh != 2 || gameCh != 3 || gameCh != 4);

    Are you sure that the ';' isn't a typo? That would cause problems.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Are you sure that the ';' isn't a typo? That would cause problems.
    Look at the subject for this thread. When using a do while loop a semi colon has to be after the while.
    Code:
    do
      {
      /* code */
      }while ( /*conditions */ );

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    estranged,

    doraiashok is right here. Basically, the only way you can get out of the loop the way you set it would be for gameCh to be equal to 1, 2, 3 and 4.... quite impossible!
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: do - while loop problem

    Originally posted by estranged
    hi every1

    im doing a do - while loop in c and to check that either 1, 2, 3 or 4 is entered and to keep on asking for those numbers till one of them is entered (do-while checks for that).

    Code:
    while(gameCh != 1 || gameCh != 2 || gameCh != 3 || gameCh != 4);
    How about a simple
    Code:
    while(gameCh < 1 || gameCh > 4);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    replacing || with && worked. thanx. and thanx to everyone else for their input.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM