3ProgramStructureII - Program Structure II Insertion of...

Info icon This 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
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 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 alc ulate  ave rag e  o f m arks : var a =  ( pars e Int(do c um e nt.F1.C Mark.value ) + pars e Int(do c um e nt.F1.EMark.value ) + pars e Int(do c um e nt.F1.MMark.value )) / 3 ale 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 alc ulate  ave rag e  o f m arks : var a =  ( pars e Int(do c um e nt.F1.C Mark.value ) + pars e Int(do c um e nt.F1.EMark.value ) + pars e Int(do c um e nt.F1.MMark.value )) / 3 ; ale rt(a) ; Can be omitted if each statement is on a separate line. Example: a = 3; b = 4; The invisible semicolons are automatically inserted.
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.
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 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.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern