4ProgramStructureIII - Program Structure III Insertion of...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 1 http://www.cs.cityu.edu.hk/~helena Program Structure III Program Structure III [Please switch off your phone] Insertion of Semicolons (;) Location of JavaScript Blocks Names of Variables and Functions Enclose statements in Braces { and } Assignment Statements Data Types (Numbers, Strings, Booleans) and Operations
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 2 http://www.cs.cityu.edu.hk/~helena The mystery of Semicolons (;) • Semicolons ( ; ) are used to separate statements from each other. But manually adding semicolon is a common practice. (In Java, C . ., adding semicolon is a must. Most programmers have adapted this rule already) • Sometimes we get trouble if we handle line break or semicolon with mistake. Don’t rely on the automatic insertion of semicolons. ( An interesting discussion is available – see HOT questions at the course web ) a  = 3 b  = 4 a le rt(a +b) ; ; ; //c a lc ula te  a ve ra g e  o f m a rks : var a  =  ( pa rs e Int(do c um e nt.F1.C Ma rk.va lue ) + pa rs e Int(do c um e nt.F1.EMa rk.va lue ) + pa rs e Int(do c um e nt.F1.MMa rk.va lue )) / 3 a le rt(a ) • The fact: JavaScript automatically inserts semicolons if the line seems to be complete. a  = 3 b  = 4 a  = 3; b  = 4; //c a lc ula te  a ve ra g e  o f m a rks : va r a  =  ( pa rs e Int(do c um e nt.F1.C Ma rk.va lue ) + / 3 ; a le rt(a ) ; • Can be omitted if each statement is on a separate line. But not suggested to omit!! Example: a  = 3; b = 4; The invisible semicolons are automatically inserted.
Background image of page 2
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 3 http://www.cs.cityu.edu.hk/~helena Location of JavaScript Blocks <htm l> <he a d>  <title >De m o </title > </he a d> <b o dy> .. </b o dy> </htm l> <s c ript type =" te xt/ja va s c ript" > </script> <script type="text/javascript"> </script> <script type="text/javascript"> </script> There can be more than 1 JavaScript blocks. • JavaScript blocks can be in : <head> </head>, and <body> </body>  But we usually put most in <head></head>. (See below) • Global variables and functions declared in one block can be accessed from other locations. Eg., the previous Counter example. Try out: Add 10 to the counter (add a add10() function in a new JavaScript block ) Global variables and functions are normally created in <head>. .</head> . Reasons: (1) The browser loads everything in the <head> tag before it starts executing any JavaScript. That means, if we put global variables and functions in <head>, then they can be used immediately when the page begins to load. (2) Easier to lookup.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 4 http://www.cs.cityu.edu.hk/~helena Names of Variables and Functions JavaScript is case-sensitive .
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 21

4ProgramStructureIII - Program Structure III Insertion of...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online