Thread: Could someone explain nested-if statement to me?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Could someone explain nested-if statement to me?

    I am confused about it.Thanks a lot.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if ( test_1 is true )
    {
        if ( test_2 is true also )
        {
           do_something( );
        }
        else /* test_2 is not true */
        {
            do_something_else( );
        }  
    }
    else /* test_1 is not true */
    {
        ... more stuff here ...
    }
    What are you having problems with specificly?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    basically all it is, is an if statement, inside of another if statment, like in the example above.
    guns dont kill people, abortion clinics kill people.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    if you just have trouble following them and the different outcomes you could draw a little tree like the bad example below

    Code:
    ex:
                                          |
                                         / \
                                        /\   /\
                                       /\   / \

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Originally posted by Draco
    if you just have trouble following them and the different outcomes you could draw a little tree like the bad example below

    Code:
    ex:
                                          |
                                         / \
                                        /\   /\
                                       /\   / \


    Does the | mean "IF"?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Originally posted by quzah
    Code:
    if ( test_1 is true )
    {
        if ( test_2 is true also )
        {
           do_something( );
        }
        else /* test_2 is not true */
        {
            do_something_else( );
        }  
    }
    else /* test_1 is not true */
    {
        ... more stuff here ...
    }
    What are you having problems with specificly?

    Quzah.




    if (A) printf("it is A");

    {

    if (a) printf("it is 1a");

    else if(b) printf("it is b");

    else if(c) printf("it is c");

    else printf("OK");

    }


    if (B) printf("it is B");

    {

    if (a) printf("it is 2a");


    else if(d) printf("it is d");

    else if(e) printf("it is e");

    else printf("OK");
    }

    if (C) printf("it is C");

    {

    if (a) printf("it is 2a");

    else if(d) printf("it is 2d");
    else if(f) printf("it is f");
    else printf("OK");
    }


    This is the problem I meet,I am confused about the same IF between A,B,C.Please help me,thank you.
    Last edited by shiyu; 02-01-2003 at 02:23 PM.

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    The first if-statements that trigger the nested sets of statements are capitol letters (A,B,C) while all of the tested letters in the nested statements are lowercase. It would help us if you posted what the upper and lowercase letter are, and any other relevant code.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @shiya

    Your code has some additional, unnecessary braces in it. For example:

    Code:
    if (A) printf("it is A");
    {
      if (a)
        printf("it is 1a");
      else if (b)
        printf("it is b");
      else if (c)
        printf("it is c");
      else
        printf("OK");
    }
    Should really be written like this:
    Code:
      if (A) 
        printf("it is A");
    
      if (a)
        printf("it is 1a");
      else if (b)
        printf("it is b");
      else if (c)
        printf("it is c");
      else
        printf("OK");
    This appears to give a different meaning to the code, albeit only visually.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    These are good responses. Think about it "nested if"

    if statements within if statements- also drawing the example out is good as well.
    Mr. C: Author and Instructor

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    if (A) printf("it is A");

    if (a) printf("it is 1a");
    else if(b) printf("it is b");
    else if(c) printf("it is c");
    else printf("OK");



    if (B) printf("it is B");

    if (a) printf("it is 2a");
    else if(d) printf("it is d");
    else if(e) printf("it is e");
    else printf("OK");




    if (C) printf("it is C");

    if (a) printf("it is 2a");
    else if(d) printf("it is 2d");
    else if(f) printf("it is f");
    else printf("OK");


    When I put these three together,It print extra "OK" every time,so how can I make these three work?thank you.

  11. #11
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    what specific input do you use?

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    My input are two letters.One is a letter from A,B,C,the other letter is from a-f.

  13. #13
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    hm... The program may be mistaking the upper and lowercase letters. Instead of just the variables you want to test, try using toupper() on the ones you want to be capital letters and tolower() on the letters that should be lowercase.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>When I put these three together,It print extra "OK" every time,so how can I make these three work?thank you.
    What do you mean by "extra". There are 3 separate if sections that end with an "else", so each of those sections will always print something. Is that where you're getting confused?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  2. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 01:51 PM
  3. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM