Thread: Simple question on while loops

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    Simple question on while loops

    why does this work
    Code:
    printf ("Please enter a sentence");
    while ((ch = getchar()) != '.') {   //i know that '\n' would terminate on new line character
    if (ch == '.' || ch == '!' || ch == '?') 
    break; 
    sentence[i++] = ch;
    but this doesnt?
    Code:
    printf ("Please enter a sentence");
    while ((ch = getchar()) != '.' || ch != '!' || ch != '?') {
    sentence[i++] = ch;
    the scond code doesn't teminate

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Can you think of a value for ch where
    Code:
    ch != '.' || ch != '!' || ch != '?'
    could possibly be false?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    think that i'm getting this wrong but from what i understand this means.....

    the value is true as long as ch does not equal '.' or '!' or '?'

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Right. And since ch can never equal all three of those at the same time, that means it is always true.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    hahahhahahahahaahah, lol, i'm an idiot!!!

    tks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple game, need help with loops and math
    By joeman in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2010, 07:47 AM
  2. while loops...a problem with my simple code?
    By niceguy in forum C Programming
    Replies: 13
    Last Post: 02-20-2008, 02:59 PM
  3. Simple C Program [for loops?]
    By mas0 in forum C Programming
    Replies: 6
    Last Post: 11-18-2006, 05:53 PM
  4. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  5. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 10:24 AM