How can I avoid the "else without if" bug? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

26th May 2017, 6:38 PM
juno.blue
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.
26th May 2017, 6:44 PM
Jim
Jim - avatar
+ 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 { } )
26th May 2017, 6:53 PM
ifl
ifl - avatar
+ 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; } }
27th May 2017, 4:58 AM
Sandeep Chatterjee