+ 1

Doubt about curly braces in if else

if sometimes we use if else, we put braces in if but not on else why does it work for example if{ ... } else if { } else

12th Aug 2018, 1:40 PM
Vivek Mishra
Vivek Mishra - avatar
3 Réponses
+ 3
If , if or else if or loops only contains one statement than {} can be omitted. Your code will be fine and there will be no error. However its good practice to wrap them inside {}. Hope this helps ☺️☺️.
12th Aug 2018, 1:55 PM
Meet Mehta
Meet Mehta - avatar
0
you should always put curly brackets after else
12th Aug 2018, 1:45 PM
Mootin Man
Mootin Man - avatar
0
Vivek Mishra In if,if else when you omit braces then it wll read only one line so braces is used when we want to execute more than one line so if u hve to execute one line its depend on u u want to put braces or not thats why error is not coming for e.g. int a=5; if(a>5) // condition is false System.out.print("hello"); /* not printed as condition is false */ System.out.print("hii"); /* it wll not effected by if statement nd it wll executed */ // hope u get it
12th Aug 2018, 3:57 PM
Harsh Agrawal
Harsh Agrawal - avatar