# Thread: If statement hw help

1. ## 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. 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.

3. 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. 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).

5. Originally Posted by AlexTank853
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. 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. Originally Posted by qny
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;
}```

8. Originally Posted by WaltP
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. 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.

10. 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. 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.