# Thread: Dangling Else error?

1. ## Dangling Else error?

I'm supposed to be able to describe a 'dangling else' error for a quiz, yet I've never seen this in any of my books, and I'm transferring into this uni so I don't know what others have covered - can someone explain this to me? I assume it has something to do with how you use if and else (duh), but I'm supposed to be able to give an example on this quiz.

2. i don't know, but my guess would be an else without an if. but don't put that.

3. The "Dangling Else":

Code:
if(x>y)
if(x>8)
x++;
else
x--;
The problem here is that the 'else' could apply to either 'if'. So basically the dangling else occurs when using nested if.. else statements without {}'s as the following would have no confusion:

Code:
if(x>y)
{
if(x>8)
x++;
else
x--;
}
Hope that make's sense.

p.s. I didn't know this before now, just Googled +"Dangling else" +"C++"

4. example of a dangling else error:

Code:
if( x==1 )
if ( y>2 )
cout<< "foo" << endl;

else
cout<< "bar" << endl;
also, http://www.google.com/search?hl=en&i...=Google+Search

5. lol, beat you to it axon

6. Makes sense. I've never had a problem understanding an If statement though, and usually whenever I do an if / else I make sure I use {} brackets.

7. yeah....damnit, should've dropped the sandwitch and typed with both hands...hehe; but my google search is priceless.

8. Originally Posted by axon
yeah....damnit, should've dropped the sandwitch and typed with both hands...hehe; but my google search is priceless.
I suck with google It always links me to pages asking me if i want to buy a domain. At least when I'm searching for C++ stuff.

9. that's ok, wasn't having a go. When I search for anything C++ related, I always add:

+"C++"

filters out a load of irrelevant crap.

dt

10. well, i got it wrong.
when i search i either am too vague and get cruddy stuff. or too specific and get nothing.

Popular pages Recent additions