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

Basic Programming by Mo Zhang

Attribute

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

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

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

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.

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

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++56 --Decrementx=--y44 x=y--54 X = ? and y = 5;

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

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

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

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 }

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:

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

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

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

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

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

Break/Continue

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

While Loop Syntax: while(condition) { … }

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

Functions function play(){ … }

Event Handler onClick onMouseOver onMouseOut onLoad

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

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

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

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

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

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

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

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();

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

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.