Thread: Single else for two ifs.

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    38

    Single else for two ifs.

    I have following code snap.

    if ( condition1)
    {
    // some lines of code
    error = somevalue1;
    if ( condition2 )
    {
    // some lines of code
    error = somevalue2;
    }
    else
    {
    error = 0;
    }
    }
    else
    {
    error = 0;
    }
    return error;

    Here both else contain same code. Is there any way that FALSE condition of any one of if statement falls to one single else statement.

    I do not like repeating same code in one subroutine.

    Please let me know..

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    int funct (int condition)
    {
         int error;
    
         if(condition1) 
         {
                error = 1;
          }
         else if(condition2) 
                 {
                      error = 2;
                 }
                else 
                    {
                        error = 0;
                     }
         return (error);
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    From what you're doing it appears that you don't really want nested if statements. With the code you listed, the second error would only be checked if the first error exists. You most likely didn't mean to do that.

    Two solutions are

    1) Nest the 2nd if statement in the else of the first if and make an else for the 2nd statement which sets error to 0. (EDIT: This is what bigtamscot just demonstrated in the previous post)

    -or-

    2) Initialize error to 0, then have completely separate if statements for error checking. Only do this if you want to be able to check for multiple errors rather than stopping after you find the first one.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    bigtamscot,

    In order to execute error = somevalue2, condition1 and condition2 both need to be true.

    In your posted code, error = 2 executes only if condition2 is TRUE and condition1 is FLASE.

    Thanks for your comments.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's one possible way to do it.
    Code:
    #include <stdio.h>
    
    #define NO_FLAG  1
    #define BAD_FLAG 2
    
    static char *err_msg[] = {
      "No errors detected",
      "No flag found",
      "Invalid flag",
    };
    
    int main ( void )
    {
      int error;
      char val[4] = {0};
    
      /*
      ** Lots of code...
      */
    
      fgets ( val, sizeof val, stdin );
    
      /*
      ** No errors if error maintains a value of 0
      */
      error = 0;
    
      if ( val[0] == '-' ) {
        if ( val[1] != 'f' )
          error = BAD_FLAG;
      }
      else
        error = NO_FLAG;
    
      /* Report error status */
      fprintf ( stderr, "error: %s\n", err_msg[error] );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    But I also have some code after first if condition. In your case, I do not think, else will execute upon TRUE on first if condition and FALSE on second if.


    if ( val[0] == '-' ) {
    if ( val[1] != 'f' )
    error = BAD_FLAG;
    }
    else
    error = NO_FLAG;

    Thanks for reply.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    That having been said, do you want ...

    Code:
    if( ( val[0] == '-' ) && ( val[3] != 'f' ) )
    {
      // Something [ Bad Flag ]
    }
    else
    {
      // Something .. else.. [ No Flag ]
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Single destination shortest path
    By dpp in forum Game Programming
    Replies: 12
    Last Post: 05-11-2009, 11:01 PM
  2. Single Entry Single Exit
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 11-11-2007, 01:34 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. concatenating single chars to multi char arrays
    By TJJ in forum C Programming
    Replies: 7
    Last Post: 11-20-2003, 04:09 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM