Thread: if if vs if else

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    if if vs if else

    I can't think of an example off hand. But is the difference between if if vs if else?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    if if?

    if you mean

    if blah
    if blah2

    then im going to go with insane confusing logic being a problem
    hooch

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Right. What is the difference between say

    if blah
    if blah2

    vs

    if blah
    else blah2

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If I do understand what you're trying to say here...

    A program will check the conditions of all if statements but only check the condition of an else (or else if) if the condition of it's corresponding if statement is false. There for making some instances of two if statements less efficient and in other cases will just yield improper results.

    Code:
     /*Consider this */
    
    if (score > 100)
       printf("You win a car.");
    if (score > 50)
       printf("You win a TV.");
    
     /* Compared to this */
    
    if (score > 100)
       printf("You win a car.");
    else if (score > 50)
       printf("You win a TV.");
    The first example would yield a prize of a Car and a TV if someone scored over 100. The second example would only yield a Car with a score of over 100 and a TV with a score between 100-51.

    Now, whatever this program is supposed to do is up to the consumer of the application, but this is an example of how two if statements and an if/else if statement could be different.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    yea sounds about right ive wondered it before too but it never came out right on paper.
    hooch

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Yes, that was what I looking for. Thanks.

Popular pages Recent additions subscribe to a feed