+ All Categories
Home > Documents > C Derived Languages C is the base upon which many build C++ conventions also influence others...

C Derived Languages C is the base upon which many build C++ conventions also influence others...

Date post: 02-Jan-2016
Category:
Upload: walter-mills
View: 214 times
Download: 2 times
Share this document with a friend
47
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have nothing in common except their “parents”
Transcript
Page 1: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

C Derived Languages

•C is the base upon which many build

•C++ conventions also influence others

•*SmallTalk is where most OOP comes

•Java and Javascript have nothing in common except their “parents”

Page 2: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Variables (or identifiers)

•Values stored in computer memory

•Value can vary over time

•Reserved Words and Keywords part of the JavaScript language--

•Variable Example:

•employeeName

Page 3: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

JavaScript Variables

•Two ways to create:

•Use var to declare the variable

•var employeeName;

•Use the assignment operator to assign the variable a value

•employeeName = “Ric”;

Page 4: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Variable name syntax rules (like C:Java,etc.)

•Cannot use reserved words & spaces

•Must begin with one of the following:

•Uppercase or lowercase ASCII letter

•Dollar sign ($) or underscore (_)

•Variables are case-sensitive

•Can use numbers, but not as first character

Page 5: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Variable Naming Conventions

•Use underscore or capitalization to separate words of an identifier

•employee_first_name

•employeeFirstName

Page 6: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

legal Illegal

my_variable$my_variable__my_variablemy_variable2myVariable

$

my%_variable2my_variablemy_#variablemy@variable~my_variablemy+variable

Page 7: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

abstractbooleanbreakbytecasecatchcharclassconst

continuedebugger

defaultdelete

dodouble

elseenumexport

extendsfalsefinal

finallyfloatfor

functiongoto

ifimplements

importin

instanceofint

interfacelong

nativenewnull

packageprivate

protectedpublicreturnshortstaticsuperswitch

synchronizedthis

throwthrows

transienttruetry

typeofvarvoid

volatilewhilewith

Page 8: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

C++ / Java Comments

•DOCUMENT your code + use it for testing

•Line comments

•// ignore all text to the end of the line

•Block comments

•/* ignore all text between these symbols */

Page 9: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

/*----------------------------------------------commented out code*/code //line commentcode

//########## separator ##########

/*______________________________________Section comment______________________________________*/

Page 10: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

a clever trick:/*----------------------------------------------*/code//--------------*/

/*----------------------------------------------*x/commented out code//---------------*/

Page 11: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

C Statements;

•Statements are ALWAYS terminated with ;

•New lines mean nothing; are ignored.

•Extra ; are harmless but it is parsed javascript and downloaded… so it has a cost

•Browsers tolerate missing ; but don’t push your luck with bad habits!

Page 12: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Strings

•String Literals

•contained in quotes “ ” or single quotes ‘ ’

•Strings can be:

•Used as “literal” values

•Assigned to a variable

•empty “”

Page 13: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Using HTML tags in strings

•Use HTML tags to format strings

•Tag must be contained inside string quotes

•var newString = ‘<div class=”me”>’;

•2 kinds of quotes saves you from having to escape quotes \”

Page 14: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Escape CharactersEscape

SquenceCharacter

\\ Backslash (\)

\nNew Line (unix-use this

one)\t Tab

\’Single quotation mark /

Apostrophe\” Double quotation mark

There are others

Page 15: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Concatenating strings

•Concatenation operator: +

•var oneString = “one”;

•var anotherString = oneString + “, two, three, …”;

•Assignment operator: +=

•var oneString = “one”;

•oneString += “, two, three, …”;

Page 16: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Numbers

•Number Literals

•just type a number

•Abstract - no int, float, or small size limits

•Math Object contains a function library from random to trig functions

Page 17: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Assignment Operators

•Syntax very similar to C/Java, but:

•var x = 'dog';

•var y = 'dog';

•x= 5;

•y= my_function();

Page 18: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Operator Description

=Assigns value of right to

left+= Adds right value to left

-= Subtracts right value to left

*= Multiplies right value to left

/= Divides right value into left

%=Divides right value into left

returns remainder

Page 19: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

var x = 4;var y = 5; x += y; // x becomes 9

var a = "Hello";var b = " World!";a += b; // a becomes "Hello World!"

Page 20: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

•!

•is it null, undefined, 0, false, “”

•()?

•( test )? if true this : if false this

•like if statement but compact, faster

•+ - * / % && || same as java

Other operators

Page 21: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Ternary conditional

•var x = ( true ) ? ‘hello world’ : 47 ;

•data types can be mixed

•Technically, it is just ? : but everybody uses the ()

Page 22: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Decision Making

if ( condition statement ) {

statement(s);

if( condition statement ) {

statement(s);

} else { statement(s); }

}

Page 23: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

conditionals only want false

•DOES NOT RUN next code block:

•if( false ){}

•if( 0 ){}

•if( “” ){}

•if( null ){}

•var x; if( x ){}

Page 24: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 25: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 26: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 27: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 28: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Comparison Operators

•Used to compare two operands for equality and if one numeric value is greater than another

•Can mix types! Auto type conversion is possible. Compare that to Java!

Page 29: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Operator Description

== true if equal

!= not equal

> greater than

< less than

>= greater or equal to

<= less or equal to

Page 30: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Common Typoo

•= is not ==

•if( x = 5 ){}

•y= ( x = 5 ) true : false;

•valid code; but not expected result

Page 31: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Custom Functions

•Function is similar to Java method

•Individual statements grouped together to form a specific procedure

•Allows you to treat the group of statements as a single unit

Page 32: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Defining Functions

1.function + name (identifier)

2.Parameter list in parentheses ()

•variables live within the function block

•Zero or more order matters

3.Code block defined by braces { }

Page 33: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

function printC(company1, company2, company3){ document.writeln(company1); document.writeln(company2); document.writeln(company3);}

Page 34: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Calling Functions

•“function invocation” or “function call”

•f()

•f( parameter1, 2, 3, 4 )

•Used in a statement

• ; denotes the end of a statement

•many javascript interpreters handle ; mistakes - it does not mean you’re correct

Page 35: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Returning a value

•A function could return nothing

•return;

•var returnValue = square(x);

•return x*x;

Page 36: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

function square(x){ return x*x;}

var result= square(4);alert(result); // displays 16alert( square(4) ); //also displays 16

Page 37: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Loopsquick review - C derived

Page 38: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

for

•Like Java, C, C++, Objective-C, C#...

•for( initialize; test; last )

•initialize is run first, only once

•test is evaluated EACH time

•Runs before the loop-- if false we stop

•last is run at the END of EACH loop

Page 39: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

var i;for( i=0; i< 50; ++i){document.writeln(”this is #” + i);}

Page 40: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

while

•while( test )

•loops until test is false

•as basic as it gets

Page 41: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

var i = 0;while( i< 50){document.writeln(”this is #” + i);++i;}

Page 42: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

do while

•do {code} while( test )

•loops until test is false

•runs code at least 1 time

•created just to make life easier

Page 43: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

var i = 0;do{document.writeln(”this is #” + i);++i;}while( i< 50);

Page 44: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

continue & break

•continue;

•Halts a looping statement and restarts the loop with the next iteration

•could be called “skip” or “blowrev”

•break;

•jumps out of loop or switch

Page 45: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 46: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Page 47: C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.

Review

•Initialization statements setup loop

•Evaluate conditional statement:

•0, false,””, null, undefined → stop

•inside loop: run statement(s) which impact conditional statement

•for( x=1; x; x+1) {alert(‘infinite loop’);}


Recommended