3ProgramStructureII - Program Structure II 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 3. Program Structure II - 1 http://www.cs.cityu.edu.hk/~helena Program Structure II Program Structure II [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 DocumentRight Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 3. Program Structure II - 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. (Refer to Q&A at course web) a = 3 b = 4 alert(a+b) a = 3 ; b = 4 ; alert(a+b) ; //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 ) + 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.value )) / 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 a  = 3; b  = 4; //c a lc ula te  a ve ra g e  o f m a rks : / 3 ; a le rt(a ) ; Can be omitted if each statement is on a separate line. 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 3. Program Structure II - 3 http://www.cs.cityu.edu.hk/~helena Location of JavaScript Blocks <html> <head>  <title>Demo</title> </head> <body> .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. </BODY> </html> <SCRIPT LANGUAGE=javascript> </SCRIPT> <SCRIPT LANGUAGE=javascript> </SCRIPT> <SCRIPT LANGUAGE=javascript> </SCRIPT> There can be more than 1 JavaScript blocks. JavaScript blocks can be placed in-between: <head> </head>, and <body> </body> 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-between the <head>. .</head> section. Reason: (1) In this way, all subsequent JavaScripts on the page will know the definition of them. The browser loads everything in the <head> tag before it starts executing any JavaScript. (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 DocumentRight Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 3. Program Structure II - 4 http://www.cs.cityu.edu.hk/~helena Names of Variables and Functions JavaScript is case-sensitive : keywords, variables, function names, etc. . must be consistent in capitalization of letters.
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.

This note was uploaded on 04/16/2011 for the course CS 1301 taught by Professor Dr.wong during the Winter '08 term at City University of Hong Kong.

Page1 / 19

3ProgramStructureII - Program Structure II 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