Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development.

Similar presentations


Presentation on theme: "Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development."— Presentation transcript:

1 Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development

2 Ingeniørhøjskolen i Århus Slide 2 af 15 This presentation Overview of JavaScript’s support of –Variables –Operators –Control structures –Functions Different examples Comments to students –From previous programming experience you will know most of it –Ex. In this course it is presumed that you now about algorithms recursion, pseudo code, control structures, loop in general and boolean expressions etc

3 Ingeniørhøjskolen i Århus Slide 3 af 15 Variable Loosely-typed language Types –Number, Boolean, String, Function, Object Variable declared by –var [= ] –Notice: no type to the var Scope –Global and local –Like other typical programming language (Nearest scope) –Local variables leaves scope and disappears

4 Ingeniørhøjskolen i Århus Slide 4 af 15 Operators Arithmetic's +, -, *, /, %, +, +=, … Equations ==, !=, NOTICE NEVER USE “=“ in equations Boolean && = AND, || = OR, ! = NOT Strings Concatenating of strings ”alfa”+”beta” = ”alfabeta”

5 Ingeniørhøjskolen i Århus Slide 5 af 15 Control structures Like other programming languages: –If-else if (<>) { … } else { … } –Switch-case switch ( ) { case : … break; … default: … }

6 Ingeniørhøjskolen i Århus Slide 6 af 15 Loops More control structures –For loop for (var counter=0; counter

7 Ingeniørhøjskolen i Århus Slide 7 af 15 Functions Used for: –Structuring and encapsulating functionality –Event handling (more when DHTML) –Example: calculations –Example: validating of user input HTML element

8 Ingeniørhøjskolen i Århus Slide 8 af 15 Examples Some Examples using functions and control structures in XHTML

9 Ingeniørhøjskolen i Århus Slide 9 af 15 Average.html 1 2 Class Average Program Click Refresh (or Reload) to run the script again Increment the counter. Calculate the average of the grades input by the user. Write the result to the XHTML document.

11 Ingeniørhøjskolen i Århus Slide 11 af 15 ForCounter.html 1 2 Counter-Controlled Repetition InitializationRepetition conditionIncrementing

12 Ingeniørhøjskolen i Århus Slide 12 af 15 Program Output

13 Ingeniørhøjskolen i Århus Slide 13 af 15 BreakTest.html 1 2 Using the break Statement in a for Structure When the value of variable count equals 5, the break statement causes program control to proceed to the first line outside the for loop.

14 Ingeniørhøjskolen i Århus Slide 14 af 15 Functions Examples –Calculations –FORM Data validating (DHTML)

15 Ingeniørhøjskolen i Århus Slide 15 af 15 SquareInt.htm 1 2 A Programmer-Defined square Function Calling function square and passing it the value of x.Variable y gets the value of variable x. The return statement passes the value of y * y back to the calling function.


Download ppt "Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development."

Similar presentations


Ads by Google