Dangling if/else beginner question

I have to find the output. I would have chosen "x is a positive number", but the guide says the answer is "x is a number". Can someone explain like I was 5, why is the word "positive" excluded?

My reasoning:

x is 5, so it doesn't meet the first if<5, skips to if(x==0), does not meet that condition, goes to the else assigned to the second if (x==0), performs the operation of cout << "positive";, then goes to the next line, and performs the operation of cout << "number";.

1
2
3
4
5
6
7
8
9
10
  int main() 
{
int x=5;
cout << "x is a ";
if (x < 0)
    if (x==0)
        cout << "non-positive";
else
    cout << "positive";
cout << "number" ;


Answers possible:
x is a positive number
x is a non-positive number
x is a number
Last edited on
The indentation is misleading. A C++ compiler does not use whitespace to associate 'elses' with 'ifs'. This is another way to express the same thing, but it uses curly braces to explicity call out the scope of the conditionals:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int main() 
{
int x=5;
cout << "x is a ";
if (x < 0)
{
    if (x==0)
    {
        cout << "non-positive";
    }
    else
    {
        cout << "positive";
    }
}
cout << "number";

What's happening is that the (non-)positive prints are being skipped since the (x<0) condition does not hold. The "else" block is associated with the innermost 'if', which does not get evaluated because of the first 'if'.
This is a good demonstration of why being explicit with your scope blocks using curly-braces is a good idea.
To demonstrate the difference, I imagine the indentation led you to read it like this:
1
2
3
4
5
6
7
8
9
10
11
12
    int x=5;
    cout << "x is a ";
    if (x < 0)
    {
        if (x==0)
            cout << "non-positive";
    }
    else
    {
        cout << "positive";
    }
    cout << "number" ;


but this is not how the compiler parses it.
Last edited on
It makes perfect sense now with the brackets. How did you know that the first "if" encompasses the second if/else? I would not know...
1) your identation is misleading, else belongs to second if, not first.
2) after x < 0 test failed, program skips all "then" actions entirely skipping to else actions or, as there is no else to next after if routine.

Code with fixed identation and added curly braces to help you understand:

1
2
3
4
5
6
7
8
9
10
11
12
int main() 
{
    int x=5;
    cout << "x is a ";
    if (x < 0) {
        if (x==0) {
            cout << "non-positive";
        } else { //x == 0
            cout << "positive";
        } //x == 0
    } // x < 0
cout << "number" <<;


EDIT: have gone to the kitchen to make tea. Probably should have refresh page before posting.
Last edited on
@MiiNiPaa:
reference to 1) The indentation is misleading on purpose... It's from a past midterm.
reference to 2) Thanks for the indentation, helps with understanding, but how did you know that the first if encompasses the second if/else? =)
Same reason that he knows to end a statement with a semicolon: Them's the C++ rules. :) That's the way the language is defined, and it is knowledge that comes along with learning the language. Perhaps someone can quote the standard? I cannot.
So basically, when this occurs:

1
2
3
if (x < 0)
    if (x==0)
    else //stuff 


The second if/else are entirely skipped if the first if is not met. Nested ifs. Trouble was: How do you recognise a nested if, from a non-nested if, since there are no indication of brackets anywhere? :/
When the compiler sees an else, it knows that the only way that this is legal C++ is if the else comes right after an if then a single statement:
1
2
if (condition)
    DoTheThing();


or the else comes after an if then a block (the block could be empty):
1
2
3
4
5
6
if (condition)
{
    DoTheThing();
    DoTheOtherThing();
    DoSomeMoreThings();
}


When it encounters the else, it is looking back to find the if that satisfies this rule.
Last edited on
Topic archived. No new replies allowed.