Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Programming by Mo Zhang. Notepad Notepad++ Dreamweaver Eclipse, …

Similar presentations


Presentation on theme: "Basic Programming by Mo Zhang. Notepad Notepad++ Dreamweaver Eclipse, …"— Presentation transcript:

1 Basic Programming by Mo Zhang

2 Notepad Notepad++ Dreamweaver Eclipse, …

3 Attribute

4 Display Strings in JS document.write(“Hello World”);

5 Comments int A = 3; int B = 11; int C; C = A + B; // the sum of two numbers.

6 Comments Con’t Multi-line comment /* Calculate the yaw Calculate the roll Calculate the pitch */

7 Variables Declaration of variables. var x = 3; Note: 1.Start the name with a letter or underscore. 2.The rest of characters have to be letters, underscores, or numbers. (Cannot use %, $, *,, &, etc.); 3.Variable names are case sensitive.

8 Different Types of Variables Data typeExampleDeclaration integers0, 1, -3, 11, 13, 24, -103int Decimal numbers Double, float Strings (text)HelloString BooleanTRUE/FALSE, 1/0Boolean, Bool Characters“0”, “1”, “c”, “%”char

9 Math Operators OperatorDescriptionExampleResult of xResult of y +Additionx=y+275 -Subtractionx=y-235 *Multiplicationx=y*2105 /Divisionx=y/22.55 % Modulus (division remainder) x=y%215 ++Incrementx=++y66 x=y Decrementx=--y44 x=y--54 X = ? and y = 5;

10 Assignment Operators OperatorExampleSame AsResult =x=y x=5 +=x+=yx=x+yx=15 -=x-=yx=x-yx=5 *=x*=yx=x*yx=50 /=x/=yx=x/yx=2 %=x%=yx=x%yx=0

11 If Statement Letting your computer make a decision if (condition) { }

12 If … Else Statement Letting your computer make a decision if (condition) { Event A; } else { Event B; }

13 If...else if...else Statement Syntax if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if neither condition1 nor condition2 is true }

14 Comparison Operators Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that x=5, the table below explains the comparison operators:

15 OperatorDescriptionComparingReturns ==equal tox==8false x==5true ===exactly equal to (equal value and equal type) x==="5"false x===5true != not equalx!=8true !== not equal (different value or different type) x!=="5"true x!==5false > greater thanx>8false < less thanx<8true >= greater than or equal tox>=8false <= less than or equal tox<=8true

16 Switch Statement if (condition 1)... If (condition 2) … If (condition 3) …

17 Switch Con’t switch(variable){ case condition1: …; break; case condition2: …; break; Default: … break; }

18 Escape Characters The teacher said “No food or beverages allowed.” \” \n

19 Loops 1.For loop 2.While loop 3.do … while loop

20 Break/Continue

21 For Loop Syntax: for (var i = 0; I < max; i++) { … }

22 While Loop Syntax: while(condition) { … }

23 Do While Loop Syntax: do { … } While(condition)

24 Functions function play(){ … }

25 Event Handler onClick onMouseOver onMouseOut onLoad

26 Using Parameters with Functions function message(value){ alert(value); }

27 Function with Multiple Parameters function sum(A, B){ int sum = A + B; }

28 The return Statement function sum(A, B){ var sum = A + B; return sum; }

29 Calling a Function From Another Function function First(){ First event; } Function Second(){ Second event; } Function body(){ First(); Second(); }

30 Global & Local Variables Global variable: any variable outside all functions. Local variable: variable inside a function.

31 Arrays Syntax: var people = new Array(“”, “”, “”);

32 Other Ways to Create Arrays var things = new Array(3); things[0] = things[1] = things[2] =

33 Array Properties and Methods 1.Size of the array; 2.Combine array; concat 3.Join array to string; var string1 = things.join(); 4.Reverse; things.reverse(); 5.Pop; things.pop(); 6.Push; things.push(“xxx”); 7.Sort in alphabetical order; things.sort();

34 Prompt var name = prompt(“Enter your name”, “”);

35 Objects A piece of data that has its own properties. For example: Human is an object. Every person has eyes, nose, hair, mouse, etc. These are the properties. Methods: what the object does. A person can drink, eat, drive a car, etc.

36


Download ppt "Basic Programming by Mo Zhang. Notepad Notepad++ Dreamweaver Eclipse, …"

Similar presentations


Ads by Google