Thread: do-while loop

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    20

    do-while loop

    Hey forum...

    Need some homework help. Working on a program that involves a menu and whole bunch of nested if statements exc. and using a do-while loop.

    Every other aspect of my program works except ending the loop. Now because i have to use a charactes for menu options, and not ints (teachers specific instructions), i want the criteria to be something like this:

    Code:
    while (selection != 'c' || selection != 'C');
    but obviously this is not working, but why? and is there a way i can get this to work? If i just make it one criteria (lower or upper case) all works well. But i want it to recognize either or. Help is much appreciated.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That condition is always true (if it's 'C' it's not 'c', and if it's 'c' it's not 'C'). You probably want &&.

    Note: “this is not working” is not a good error report. You neglected to mention what you expected to happen and what was actually happening. This is an easily-recognizable problem, so I guessed at what was going on, but in the future, more precise error reports will make finding problems much easier.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    "selection" is lower-case 'c':

    Code:
    while (selection != 'c' || selection != 'C');
    
    selection != 'c' // False
    selection != 'C' // True
    
    False OR True = True ... loop continues
    "selection" is upper-case 'C':

    Code:
    while (selection != 'c' || selection != 'C');
    
    selection != 'c' // True
    selection != 'C' // False
    
    True OR False = True ... loop continues
    "selection" is upper-case 'A':

    Code:
    while (selection != 'c' || selection != 'C');
    
    selection != 'c' // True
    selection != 'C' // True
    
    True OR True = True ... loop continues
    See the pattern?



    You did not specify exactly how you wanted the logic, whether an input of 'c' exits the loop or an input other than 'c' exits the loop. If it's the former, perhaps you should try logical AND instead.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    thanks for the help and insight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  2. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  3. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM