Thread: if statement with 2 conditions

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    4

    if statement with 2 conditions

    Hi all,
    I am currently trying to learn C and would appreciate any help...

    Can anyone please explain why the following code will not work?
    The if statement if (yn!='y' || yn!='n') always becomes true!

    Code:
    #include<stdio.h>
    
    main()
    {
        char yn;
        while(yn!='e') {
            printf("enter y/n? ");
            scanf(" %c", &yn);
                if (yn=='e') {
                    printf("goodbye\n");
                    break;
                }        
    
                if (yn!='y' || yn!='n') {
                    printf("error, enter y or n!\n");
                }
        }
    return 0;
    }
    I did manage to replicate the code with nested if's but after much reading it's better to write more concise code. Correct me if I am wrong?

    Code:
    #include<stdio.h>
    
    main()
    {
        char yn;
        while(yn!='e') {
            printf("enter y/n? ");
            scanf(" %c", &yn);
                if (yn=='e') {
                    printf("goodbye\n");
                    break;
                }            
    
                if (yn!='y') {
                    if (yn!='n') {
                        printf("error, enter y or n!\n");
                    }
                }
        }
    return 0;
    }
    Last edited by robando; 04-12-2015 at 02:27 AM.

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    The code is little bit confusing in the sense in the first while(yn != 'e') you have not yet done any scanf. once you have done scanf you are breaking from the while loop. In the
    Code:
      if (yn!='y' || yn!='n') {     
               printf("error, enter y or n!\n");
             }
    the condition is always true since if you enter 'y' then yn != 'y' is False or 0 but yn != 'n' is True or 1 and hence the result is 1 always. If you enter 'n' the same result happens.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    4
    Hi Satya,

    The while loop is simply to reiterate the program for testing. Admittedly it's me being a bit lazy. Obviously typing 'e' will exit the program.
    Is my while loop not a good way to create a loop?
    If not can you please provide a better example?

    The if statement is within the while loop.
    How is it breaking form the while loop once scanf has been done?

    After thinking about your explanation about if (yn!='y' || yn!='n') you are absolutly correct, thanks.

    Thank you for you help
    Last edited by robando; 04-12-2015 at 04:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - If statement with multiple conditions
    By trintiin in forum C Programming
    Replies: 3
    Last Post: 02-27-2013, 01:49 AM
  2. if and while conditions
    By firehydrant in forum C Programming
    Replies: 12
    Last Post: 03-05-2011, 03:07 AM
  3. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  4. IF statement - to conditions
    By cornacum in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 08:39 PM
  5. If statement conditions
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:55 AM