basic programming by mo zhang. notepad notepad++ dreamweaver eclipse, …

36
Basic Programming by Mo Zhang

Upload: anita-wimsett

Post on 01-Apr-2015

271 views

Category:

Documents


2 download

TRANSCRIPT

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

Basic Programming

byMo Zhang

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

• Notepad• Notepad++• Dreamweaver• Eclipse, • …

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

Attribute<script>

</script>

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

Display Strings in JS

document.write(“Hello World”);

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

Comments

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

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

Comments Con’t

Multi-line comment/*Calculate the yawCalculate the rollCalculate the pitch

*/

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

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.

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

Different Types of VariablesData type Example Declaration

integers 0, 1, -3, 11, 13, 24, -103 int

Decimal numbers 3.1415926 Double, float

Strings (text) Hello String

Boolean TRUE/FALSE , 1/0 Boolean, Bool

Characters “0”, “1”, “c”, “%” char

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

Math Operators

Operator Description Example Result of x Result of y+ Addition x=y+2 7 5- Subtraction x=y-2 3 5* Multiplication x=y*2 10 5/ Division x=y/2 2.5 5% Modulus (division remainder) x=y%2 1 5++ Increment x=++y 6 6

x=y++ 5 6-- Decrement x=--y 4 4

x=y-- 5 4

X = ? and y = 5;

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

Assignment OperatorsOperator Example Same As Result= x=y x=5+= x+=y x=x+y x=15-= x-=y x=x-y x=5*= x*=y x=x*y x=50/= x/=y x=x/y x=2%= x%=y x=x%y x=0

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

If Statement

Letting your computer make a decision

if (condition){

}

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

If … Else Statement

Letting your computer make a decision

if (condition){Event A;}else{Event B;}

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

If...else if...else StatementSyntax

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 }

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

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:

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

Operator Description Comparing Returns== equal to x==8 false

x==5 true=== exactly equal to (equal

value and equal type)x==="5" falsex===5 true

!= not equal x!=8 true!== not equal (different value

or different type)x!=="5" truex!==5 false

> greater than x>8 false< less than x<8 true>= greater than or equal to x>=8 false<= less than or equal to x<=8 true

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

Switch Statement

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

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

Switch Con’tswitch(variable){

case condition1:…;break;

case condition2:…;break;

Default: …

break;}

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

Escape Characters

The teacher said “No food or beverages allowed.”

\”

\n

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

Loops

1. For loop2. While loop3. do … while loop

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

Break/Continue

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

For Loop

Syntax:for (var i = 0; I < max; i++){

………

}

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

While Loop

Syntax:while(condition){

}

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

Do While Loop

Syntax:do{

}While(condition)

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

Functions

function play(){

……

}

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

Event Handler

<input type =‘button’ value=‘click me’ onClick=‘alert()’ />

onClickonMouseOveronMouseOutonLoad

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

Using Parameters with Functions

function message(value){

alert(value);

}

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

Function with Multiple Parameters

function sum(A, B){int sum = A + B;

}

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

The return Statement

function sum(A, B){var sum = A + B;

return sum;

}

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

Calling a Function From Another Function

function First(){First event;

}

Function Second(){Second event;

}

Function body(){First();Second();

}

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

Global & Local Variables

Global variable: any variable outside all functions.

Local variable: variable inside a function.

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

Arrays

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

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

Other Ways to Create Arrays

var things = new Array(3);things[0] = things[1] = things[2] =

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

Array Properties and Methods

1. Size of the array;2. Combine array; concat3. 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();

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

Prompt

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

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

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.

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