-1

if statement has an open {} close brackets

  if(example){
      return example;
  }else{
      return example;
  }

if statement without open {} close brackets

  if(example)
      return example;
  else
      return example;

Can anyone explain to me about the pros and cons of using them? Thanks in advance :)

New contributor
Jesper Martinez is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • I tried to migrate this to Stack Overflow where it belongs, but the migration failed. Programming questions are better suited for Stack Overflow than here. – Stephen Ostermiller May 22 at 17:32
  • @StephenOstermiller I apologize for my question, that's why I tried to ask here because I could not ask there anymore :( sorry for that kind of stuff. – Jesper Martinez May 22 at 17:39
2

There is no difference in PHP or JavaScript for example; except that, with the brackets, it is easier to add instructions later without breaking your code (you are less likely to forget to add the brackets that become mandatory).

| improve this answer | |
New contributor
Éric is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    also, personally, I find it a bit easier to visualise the start and end of the if or the else. – Steve May 22 at 23:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.