+ 1
How can I avoid the "else without if" bug?
Every time I try to write a program using if-else statements, the same problem keeps occurring. Any idea how to fix the problem?
3 Answers
+ 12
What about setting up the structure first? i.e. set out the if, else and brackets for each layer of nested conditional, to make sure that everything matches up. I've found that helpful when things get complicated.
+ 3
making sure the code is properly indented helps a lot spotting the error
Often the error may be due to missing block delimiters : make sure you have the { } present if there are more than one statement in a block
(i find it easier to always add all the { } )
+ 2
Jim and ifi are correct. it's more like a coding practice. add brackets first then write code inside if and else. do proper indention to clearly understand which if is nested.
if you are getting continuous else without if error then you are perhaps using wrong syntax correct is
if(---){
codes;
}else{
if(---){
codes;
}else{
codes;
}
}