Basic Programming by Mo Zhang. Notepad Notepad++ Dreamweaver Eclipse, …
-
Upload
anita-wimsett -
Category
Documents
-
view
271 -
download
2
Transcript of Basic Programming by Mo Zhang. Notepad Notepad++ Dreamweaver Eclipse, …
Basic Programming
byMo Zhang
• Notepad• Notepad++• Dreamweaver• Eclipse, • …
Attribute<script>
</script>
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 yawCalculate the rollCalculate 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 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
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;
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
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 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 }
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:
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
Switch Statement
if (condition 1)...If (condition 2)…If (condition 3)…
Switch Con’tswitch(variable){
case condition1:…;break;
case condition2:…;break;
Default: …
break;}
Escape Characters
The teacher said “No food or beverages allowed.”
\”
\n
Loops
1. For loop2. While loop3. 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
<input type =‘button’ value=‘click me’ onClick=‘alert()’ />
onClickonMouseOveronMouseOutonLoad
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; 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();
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.