Like Tree4Likes
  • 3 Post By qny
  • 1 Post By whiteflags

If statement hw help

This is a discussion on If statement hw help within the C Programming forums, part of the General Programming Boards category; Explain the difference between 1 and 2 and give the value for x in both when x is 1. 1) ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Talking If statement hw help

    Explain the difference between 1 and 2 and give the value for x in both when x is 1.

    1)
    if ( x >= 0)
    x = x + 1;
    else if ( x >= 1)
    x = x + 2;
    2)
    if ( x >= 0)
    x = x + 1;
    if ( x >= 1)
    x = x + 2;

    I came up with they're are both the same and x simply would be 3 for both of them. Im almost sure I am wrong however I feel like i don't fully understand the entire concept of why, can anyone give me the x value for both and explain how they are different ( minus the obvious that one says else).

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Unfortunately, this site has a homework policy, which amounts to "we might help with specific aspects you don't understand, but won't do you homework unless you've had a good go at it".

    You are incorrect in saying they will both give the same result. Consider whether the "x >= 1" is even tested in both examples.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    23
    I appreciate the heads up, would x be 3 for question 1 and x be 4 for question 2? If you cant answer i understand, i do understand hat the else if does but what if it is both if statements are satisfied?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    You've got one of the results right, and the other wrong.

    Consider the difference between two sets of sequential spoken instructions "If you are tall, paint the wall white, otherwise paint the wall red." and "If you are tall, paint the wall white. Paint the wall red." The only difference is that the word "otherwise" (which, in C, is expressed as "else") is replaced with a punctuation character (full stop). That little change potentially changes the outcome.

    If you follow the first instruction the wall will finish white if you are tall, and red otherwise. If you follow the second instruction, the wall will always finish up being red and, if you are tall, it will have a white undercoat. (Assuming you wait for one layer of paint to dry before applying another).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by AlexTank853 View Post
    I appreciate the heads up, would x be 3 for question 1 and x be 4 for question 2? If you cant answer i understand, i do understand hat the else if does but what if it is both if statements are satisfied?
    In #1, the else if part would not be reached. The if() else , dominates the logic, over the if() inside the else.

  6. #6
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    With a better indentation and braces your codes would be written like this:

    Code:
    if (x >= 0) {
        x = x + 1;
    } else {
        if ( x >= 1) {
            x = x + 2;
        }
    }
    Code:
    if (x >= 0) {
        x = x + 1;
    }
    if (x >= 1) {
        x = x + 2;
    }
    See if you can spot the difference now

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by qny View Post
    With a better indentation
    Or my personal preference:
    Code:
    if (x >= 0) 
    {
        x = x + 1;
    } 
    else 
    {
        if ( x >= 1) 
        {
            x = x + 2;
        }
    }
    Code:
    if (x >= 0) 
    {
        x = x + 1;
    }
    if (x >= 1) 
    {
        x = x + 2;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by WaltP View Post
    Or my personal preference:
    Mine too,but i noticed that netbeans for example ,when asked to do an auto format it selects gny's format..So,maybe both me and you should switch format :/

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by std10093
    Mine too,but i noticed that netbeans for example ,when asked to do an auto format it selects gny's format..So,maybe both me and you should switch format :/
    Or you can switch Netbean's auto format's format to match your preference.

    Both indent styles are readable, so it does not matter as long as you are consistent.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    I do not want to start a style war. [b]I agree the important thing is to be consistent.[/]

    I just found out another reason to prefer my style a couple days ago: I was looking at a friend's code and looked at his if
    Code:
    if (a == b)
    {
        /* two or */
        /* three lines of code */
    }
    else
    {
        /* two or */
        /* three lines of code */
    }
    and my brain promptly ignored the else part, because I glimpsed it as another statement.

    This just proves I wasn't paying enough attention to my friend's code; not that the style is worse than any other style.
    With my style there is a more visual indication that the else belongs to the if.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Whereas I prefer not to sandwich elses with braces because I think that putting else on a different line delimits code blocks better, even if I leave the opening brace at the end of a line.
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  2. What does this statement mean?
    By starcatcher in forum C Programming
    Replies: 3
    Last Post: 02-03-2008, 09:55 AM
  3. If statement help...
    By bradszy in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2008, 02:08 AM
  4. if statement
    By goran00 in forum C Programming
    Replies: 16
    Last Post: 01-26-2008, 01:49 PM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21