O'Reilly Forums: Just Started The Book. Where Are The Curly Brackets? - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Just Started The Book. Where Are The Curly Brackets?

#1 User is offline   remdawg 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 11-April 08

Posted 13 October 2011 - 03:23 AM

I want to make sure that I'm not going to get into a bad habit, but I just completed chapt.2 and none of the IF statements used are in curly brackets. Skimming through the book to the next chapter I see one exercise where they are used.
Can I get some feedback from someone that has read the book and assure me that the book is A-OKAY when it comes to good coding habits?


Thanks

This post has been edited by remdawg: 13 October 2011 - 03:32 AM

0

#2 User is offline   cxgslegend 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 30-June 11

Posted 13 October 2011 - 11:23 AM

View Postremdawg, on 13 October 2011 - 03:23 AM, said:

I want to make sure that I'm not going to get into a bad habit, but I just completed chapt.2 and none of the IF statements used are in curly brackets. Skimming through the book to the next chapter I see one exercise where they are used.
Can I get some feedback from someone that has read the book and assure me that the book is A-OKAY when it comes to good coding habits?


Thanks


Your right, you should always use curly brackets when you use the if statements. But the reason they don't use them in the book sometimes is because if the "if" statement only has one statement under it, and no curly brackets, it will only execute that one statement. for example

var you = "awesome";

if (you == "awesome") {
   do this;
   and this;
}
other code;
other code;

but if you take off the curly brackets 

if (you == "awesome") 
   do this;
   and this; //this code will be won't be considered part of the if statement


but as you said its a bad idea to leave off the curly brackets, because if there is more than one statement in the if statement it won't be included in the if statement.

hope that helped :-)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users