Jump to content


Photo

Just Started The Book. Where Are The Curly Brackets?


  • Please log in to reply
1 reply to this topic

#1 remdawg

remdawg

    New Member

  • Members
  • Pip
  • 6 posts

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

Edited by remdawg, 13 October 2011 - 03:32 AM.


#2 cxgslegend

cxgslegend

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 October 2011 - 11: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


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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users