Thread: Bad practice to use if within another if?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Bad practice to use if within another if?

    Hi,

    Im designing a program based on a flowchart as exam practice. This flowchart has a first check (is i<10) and if yes, then has another check ( is c == '\0' )

    My solution was going to involve an if within an if, but i was wondering if that is bad practice, or if there is a more suitable way of doing it.

    EDIT on a side note, if line[10]="Fantastic" is being used in the code, since fantastic is only 9 letters could i not use line[8] instead?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not bad practice to use nested if statements. Consider:
    Code:
    void foo( int *bar )
    {
        if( bar != NULL ) /* if it's NULL we can't use it, so we have to check that... */
        {
            if( *bar < 10 ) /* now that we know we CAN use it, do whatever... */
            {
                 /* do stuff... */
            }
        }
    }
    That's just a simple example. If done cleanly, it's actually good practice.

    As to your second question, it depends what "line" is defined as. Assuming you're just defining an array:
    Code:
    char line[ 10 ] = "Fantastic";
    Then you must use at least 10 characters here, since you're treating it like a string. Remember strings all have an extra character on the end that you aren't seeing. So "Fantastic" is actually 10 characters, because it includes the null character ( \0 ).

    Furthermore, array indexing starts out at zero, and continues through size-1. So you wouldn't have 0 - 8, you'd have 0 - 7 to work with, so even if you implicitly assigned the characters one at a time, and didn't treat it as a string, you'd still be too small at 8.


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

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I would agree with everything quzah said. A lot of nested statement can be bad practice, but it all depend on how you do it. Is it clear? Is it really the best way to do it?

    I noticed in a couple of your posts - you're confusing the way arrays get indexed. When you're declaring an array, you specify the total number of elements. When you're USING that array, you start counting at 0, so that last element is one less than how many elements there are (the last element in array[10] is array[9]). You have to take that an extra step when working with strings because of the null character.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by spadez View Post
    Hi,

    Im designing a program based on a flowchart as exam practice. This flowchart has a first check (is i<10) and if yes, then has another check ( is c == '\0' )

    My solution was going to involve an if within an if, but i was wondering if that is bad practice, or if there is a more suitable way of doing it.

    EDIT on a side note, if line[10]="Fantastic" is being used in the code, since fantastic is only 9 letters could i not use line[8] instead?
    You can simply check them both at the same time.

    Code:
    if(i < 10 && c == '\0')
    {
       //if both are true then code gets executed
       // if either one is false then no code
       // between the { and } are executed
    }
    Nested if's are ok when used properly.

    char line[10] = "Fantastic" <<<< is wrong
    char line[] = "Fantastic" <<<< is proper

    F| a| n| t| a| s| t| i| c|\0
    0| 1| 2| 3| 4| 5|6|7|8 |9


    If you wanted the if statement to check the last spot it would be

    if(line[9] == '\0') And this would be true.
    Last edited by strictlyC; 04-17-2009 at 11:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad practice or acceptable?
    By Noose in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2004, 01:43 AM
  2. Replies: 11
    Last Post: 11-13-2002, 01:29 PM
  3. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 08:37 AM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM