Principles of Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Principles of Programming

Description:

var string = 'hello world'; var arr = array( 1, 2, 3 ); 8/25/09. 4. Complex Types. Vector ... var f = function( x ) { alert( x ); } this method is not very ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 16
Provided by: you81
Category:

less

Transcript and Presenter's Notes

Title: Principles of Programming


1
Principles of Programming
With examples in JavaScript and odd pseudo-code
  • Zachary Young

2
Contents
  • Variables
  • Operators
  • Conditionals
  • Loops
  • Functions
  • Programming Habits
  • Introduction to Complexity Analysis

3
Common Variable Types
  • Integer
  • Float
  • Boolean
  • Character
  • String
  • Array
  • var integer 100
  • var float 3.1415
  • var boolean TRUE
  • var character a
  • var string hello world
  • var arr array( 1, 2, 3 )

4
Complex Types
  • Vector
  • (Linked) List
  • Stack
  • Queue

Arrays are vectors in JavaScript Not many complex
types in JavaScript, but one could create them
5
Complex Types 2
  • Different types have different advantages and
    disadvantages
  • Vectors are good for random access, but not for
    dynamic resizing
  • Lists work well when dynamic resizing is
    important, but do not work well with random
    access.

6
Objects
  • Represent objects (items) in the real world.
  • Structures Classes in most languages
  • Simply Objects in JavaScript
  • Document Object Model (DOM) in JavaScript

7
Variable Typing
  • Strong Typing
  • C
  • A variable is declared to be of a certain type
    and does not change except by coercion.
  • Weak Typing
  • Most web languages (including JavaScript)
  • Variables are only briefly associated with a type.

8
Operators
  • Mathematical
  • Addition ( )
  • Subtraction ( - )
  • Multiplication ( )
  • Division ( / )
  • Modulus ( )
  • Assignment ( )
  • Variations ( , --, , - )
  • Conditional
  • And ( )
  • Or ( )
  • Not ( ! )
  • Equal ( )
  • Not Equal ( !, ltgt )

9
Conditional Statements
  • If Elseif Else
  • stmt1 ? stmt2 third
  • If stmt1 is true, then do stmt2, otherwise do
    stmt3
  • Switch statements
  • if ( zach great )
  • document.write( correct )
  • elseif ( zach awesome )
  • document.write( right! )
  • else
  • document.write( WRONG )
  • printval ( (zach great ) ? correct (
    zach awesome ) ? right WRONG )
  • document.write( printval )

10
Looping
  • For
  • While
  • Do While
  • for ( lcv 0 lcv lt 100lcv)
  • document.write( lcv )
  • while( lcv lt 100 )
  • document.write( lcv )
  • lcv
  • lcv 0
  • do
  • document.write( lcv )
  • lcv
  • while( lcv lt 100 )

11
Functions
  • Provide modular way of performing repeated tasks
  • One task per function
  • Usually the shorter the better
  • function zachgreat ( var zach )
  • alert( zach )
  • var f function( x ) alert( x )
  • this method is not very useful in JavaScript

12
Passing Variables
  • Pass-by-reference
  • Memory address of the original variable is passed
    to the function.
  • (not available in JavaScript)
  • Pass-by-value
  • Copy of the variable is passed to the function.

13
Good Programming Habits
  • Plan before you code
  • Use descriptive variable names
  • Use comments
  • Small modular functions
  • Use consistent style
  • Brackets
  • Naming conventions

14
Complexity Analysis
  • Big O Notation
  • Analyzes worst case scenario
  • Denoted by
  • O( 1 ), O( n ), O( n2 ), etc
  • Usually only worry about highest order term
  • limn-gt8 O( n3 n2 ) n3

15
Conclusion
  • Plan first!
  • Be consistent!
  • Delve deeper!
  • Regular expressions
  • Context-free grammars
  • Design patterns
Write a Comment
User Comments (0)
About PowerShow.com